Skip to content

Commit

Permalink
middleware tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersonQ committed Nov 3, 2019
1 parent b15f58e commit 805c6f8
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions middlewares/middlewares_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package middlewares

import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/AndersonQ/go-skeleton/handlers"
)

func TestJsonResponse(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest("ignore", "/ignore", nil)

h := JsonResponse(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
h.ServeHTTP(w, r)

got := w.Result().Header.Get(handlers.ContentType)

if got != handlers.ContentTypeJSON {
t.Errorf("got header %s: %s, want: %s", handlers.ContentType, got, handlers.ContentTypeJSON)
}
}

func TestTimeoutWrapper(t *testing.T) {
want := http.StatusServiceUnavailable
timeout := time.Millisecond

w := httptest.NewRecorder()
r := httptest.NewRequest("ignore", "/ignore", nil)

h := TimeoutWrapper(timeout)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(2 * timeout)
}))

h.ServeHTTP(w, r)

got := w.Result().StatusCode
if got != want {
t.Errorf("got http status: %d, want: %d", got, want)
}
}

0 comments on commit 805c6f8

Please sign in to comment.