Skip to content

Commit

Permalink
Merge pull request kubernetes#644 from kelseyhightower/improve_httplo…
Browse files Browse the repository at this point in the history
…g_test_coverage

httplog: Improve test coverage
  • Loading branch information
lavalamp committed Jul 27, 2014
2 parents 8a5cc87 + 200b2c0 commit ab91e07
Showing 1 changed file with 78 additions and 11 deletions.
89 changes: 78 additions & 11 deletions pkg/httplog/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestHandler(t *testing.T) {
})
req, err := http.NewRequest("GET", "http://example.com/kube", nil)
if err != nil {
t.Errorf("Unexpected error: %#v", err)
t.Errorf("Unexpected error: %v", err)
}
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
Expand All @@ -46,17 +46,16 @@ func TestHandler(t *testing.T) {
}
}

var statusTestTable = []struct {
status int
statuses []int
want bool
}{
{http.StatusOK, []int{}, true},
{http.StatusOK, []int{http.StatusOK}, false},
{http.StatusCreated, []int{http.StatusOK, http.StatusAccepted}, true},
}

func TestStatusIsNot(t *testing.T) {
statusTestTable := []struct {
status int
statuses []int
want bool
}{
{http.StatusOK, []int{}, true},
{http.StatusOK, []int{http.StatusOK}, false},
{http.StatusCreated, []int{http.StatusOK, http.StatusAccepted}, true},
}
for _, tt := range statusTestTable {
sp := StatusIsNot(tt.statuses...)
got := sp(tt.status)
Expand All @@ -65,3 +64,71 @@ func TestStatusIsNot(t *testing.T) {
}
}
}

func TestMakeLogged(t *testing.T) {
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
handler := func(w http.ResponseWriter, r *http.Request) {
MakeLogged(req, &w)
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected MakeLogged to panic")
}
}()
MakeLogged(req, &w)
}
w := httptest.NewRecorder()
handler(w, req)
}

func TestLogOf(t *testing.T) {
logOfTests := []bool{true, false}
for _, makeLogger := range logOfTests {
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
handler := func(w http.ResponseWriter, r *http.Request) {
var want *respLogger
if makeLogger {
want = MakeLogged(req, &w)
} else {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected LogOf to panic")
}
}()
}
got := LogOf(w)
if want != got {
t.Errorf("Expected %v, got %v", want, got)
}
}
w := httptest.NewRecorder()
handler(w, req)
}
}

func TestUnlogged(t *testing.T) {
unloggedTests := []bool{true, false}
for _, makeLogger := range unloggedTests {
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
handler := func(w http.ResponseWriter, r *http.Request) {
want := w
if makeLogger {
MakeLogged(req, &w)
}
got := Unlogged(w)
if want != got {
t.Errorf("Expected %v, got %v", want, got)
}
}
w := httptest.NewRecorder()
handler(w, req)
}
}

0 comments on commit ab91e07

Please sign in to comment.