From e0173ec4c7f1ebf9259d877fb8a5b492d2aa1842 Mon Sep 17 00:00:00 2001 From: Michael Schoebel Date: Sun, 10 May 2015 14:41:48 +0200 Subject: [PATCH] internal middleware: added tests --- middleware/internal/internal_test.go | 64 ++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 middleware/internal/internal_test.go diff --git a/middleware/internal/internal_test.go b/middleware/internal/internal_test.go new file mode 100644 index 00000000..392a3d17 --- /dev/null +++ b/middleware/internal/internal_test.go @@ -0,0 +1,64 @@ +package internal + +import ( + "fmt" + "net/http" + "net/http/httptest" + "testing" + + "github.com/mholt/caddy/middleware" +) + +func TestInternal(t *testing.T) { + im := Internal{ + Next: middleware.HandlerFunc(internalTestHandlerFunc), + Paths: []string{"/internal"}, + } + + tests := []struct { + url string + expectedCode int + expectedBody string + }{ + {"/internal", http.StatusNotFound, ""}, + + {"/public", 0, "/public"}, + {"/public/internal", 0, "/public/internal"}, + + {"/redirect", 0, "/internal"}, + + {"/cycle", http.StatusInternalServerError, ""}, + } + + for i, test := range tests { + req, err := http.NewRequest("GET", test.url, nil) + if err != nil { + t.Fatalf("Test %d: Could not create HTTP request: %v", i, err) + } + + rec := httptest.NewRecorder() + code, err := im.ServeHTTP(rec, req) + + if code != test.expectedCode { + t.Errorf("Test %d: Expected status code %d for %s, but got %d", + i, test.expectedCode, test.url, code) + } + if rec.Body.String() != test.expectedBody { + t.Errorf("Test %d: Expected body '%s' for %s, but got '%s'", + i, test.expectedBody, test.url, rec.Body.String()) + } + } +} + +func internalTestHandlerFunc(w http.ResponseWriter, r *http.Request) (int, error) { + switch r.URL.Path { + case "/redirect": + w.Header().Set("X-Accel-Redirect", "/internal") + case "/cycle": + w.Header().Set("X-Accel-Redirect", "/cycle") + } + w.WriteHeader(http.StatusOK) + fmt.Fprintf(w, r.URL.String()) + + return 0, nil +}