git.iko.soy/typeable/oauth2-proxy
on request-rules help sign in
81 lines 2.1 kB history raw
1package upstream
3import (
4 "crypto/rand"
5 "io"
6 "net/http"
7 "net/http/httptest"
9 . "github.com/onsi/ginkgo"
10 . "github.com/onsi/ginkgo/extensions/table"
11 . "github.com/onsi/gomega"
14var _ = Describe("Static Response Suite", func() {
15 const authenticated = "Authenticated"
16 var id string
18 BeforeEach(func() {
19 // Generate a random id before each test to check the GAP-Upstream-Address
20 // is being set correctly
21 idBytes := make([]byte, 16)
22 _, err := io.ReadFull(rand.Reader, idBytes)
23 Expect(err).ToNot(HaveOccurred())
24 id = string(idBytes)
25 })
27 type serveHTTPTableInput struct {
28 requestPath string
29 staticCode int
30 expectedBody string
31 expectedCode int
32 }
34 DescribeTable("staticResponse ServeHTTP",
35 func(in *serveHTTPTableInput) {
36 var code *int
37 if in.staticCode != 0 {
38 code = &in.staticCode
39 }
40 handler := newStaticResponseHandler(id, code)
42 req := httptest.NewRequest("", in.requestPath, nil)
43 rw := httptest.NewRecorder()
44 handler.ServeHTTP(rw, req)
46 Expect(rw.Header().Get("GAP-Upstream-Address")).To(Equal(id))
47 Expect(rw.Code).To(Equal(in.expectedCode))
48 Expect(rw.Body.String()).To(Equal(in.expectedBody))
49 },
50 Entry("with no given code", &serveHTTPTableInput{
51 requestPath: "/",
52 staticCode: 0, // Placeholder for nil
53 expectedBody: authenticated,
54 expectedCode: http.StatusOK,
55 }),
56 Entry("with status OK", &serveHTTPTableInput{
57 requestPath: "/abc",
58 staticCode: http.StatusOK,
59 expectedBody: authenticated,
60 expectedCode: http.StatusOK,
61 }),
62 Entry("with status NoContent", &serveHTTPTableInput{
63 requestPath: "/def",
64 staticCode: http.StatusNoContent,
65 expectedBody: authenticated,
66 expectedCode: http.StatusNoContent,
67 }),
68 Entry("with status NotFound", &serveHTTPTableInput{
69 requestPath: "/ghi",
70 staticCode: http.StatusNotFound,
71 expectedBody: authenticated,
72 expectedCode: http.StatusNotFound,
73 }),
74 Entry("with status Teapot", &serveHTTPTableInput{
75 requestPath: "/jkl",
76 staticCode: http.StatusTeapot,
77 expectedBody: authenticated,
78 expectedCode: http.StatusTeapot,
79 }),
80 )
81})