Skip to content

Commit

Permalink
WIP: possible test?
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanhitt committed Mar 25, 2024
1 parent 55c025c commit cc2c220
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,78 @@ func dummyMiddleware(handler http.Handler) http.Handler {
})
}

// orderMiddleware sets the X-Test-Order Header on the request and
// X-Test-Response header on the response. It is
// used to test the order execution of our middleware
func orderMiddleware(s string) func(http.Handler) http.Handler {
return func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
orderValue := strings.Split(r.Header.Get("X-Test-Order"), ",")
orderValue = append(orderValue, s)

r.Header.Set("X-Test-Order", strings.Join(orderValue, ","))
w.Header().Set("X-Test-Response", "response")
handler.ServeHTTP(w, r)
})
}
}

// TestUse is used to mainly test the ordering of middleware execution
func TestUse(t *testing.T) {
t.Run("base", func(t *testing.T) {
s := NewServer()
Use(s, orderMiddleware("First!"))
Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
return "test", nil
})

r := httptest.NewRequest(http.MethodGet, "/test", nil)
r.Header.Set("X-Test-Order", "Start!")
w := httptest.NewRecorder()

s.Mux.ServeHTTP(w, r)

orderValue := strings.Split(r.Header.Get("X-Test-Order"), ",")
require.Equal(t, []string{"Start!", "First!"}, orderValue)
})

t.Run("multiple uses of Use", func(t *testing.T) {
s := NewServer()
Use(s, orderMiddleware("First!"))
Use(s, orderMiddleware("Second!"))
Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
return "test", nil
})

r := httptest.NewRequest(http.MethodGet, "/test", nil)
r.Header.Set("X-Test-Order", "Start!")
w := httptest.NewRecorder()

s.Mux.ServeHTTP(w, r)

orderValue := strings.Split(r.Header.Get("X-Test-Order"), ",")
require.Equal(t, []string{"Start!", "First!", "Second!"}, orderValue)
})

t.Run("variadic use of Use", func(t *testing.T) {
s := NewServer()
Use(s, orderMiddleware("First!"))
Use(s, orderMiddleware("Second!"), orderMiddleware("Third!"))
Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
return "test", nil
})

r := httptest.NewRequest(http.MethodGet, "/test", nil)
r.Header.Set("X-Test-Order", "Start!")
w := httptest.NewRecorder()

s.Mux.ServeHTTP(w, r)

orderValue := strings.Split(r.Header.Get("X-Test-Order"), ",")
require.Equal(t, []string{"Start!", "First!", "Second!", "Third!"}, orderValue)
})
}

func TestUseStd(t *testing.T) {
s := NewServer()
UseStd(s, dummyMiddleware)
Expand Down

0 comments on commit cc2c220

Please sign in to comment.