| 39 | var base64EncodedBodyRequest []byte |
| 40 | |
| 41 | func TestWrap(t *testing.T) { |
| 42 | for name, params := range map[string]struct { |
| 43 | input []byte |
| 44 | handler http.HandlerFunc |
| 45 | detectContentType bool |
| 46 | expectStatus int |
| 47 | expectBody string |
| 48 | expectHeaders map[string]string |
| 49 | expectCookies []string |
| 50 | }{ |
| 51 | "hello": { |
| 52 | input: helloRequest, |
| 53 | handler: func(w http.ResponseWriter, r *http.Request) { |
| 54 | w.Header().Add("Hello", "world1") |
| 55 | w.Header().Add("Hello", "world2") |
| 56 | http.SetCookie(w, &http.Cookie{Name: "yummy", Value: "cookie"}) |
| 57 | http.SetCookie(w, &http.Cookie{Name: "yummy", Value: "cake"}) |
| 58 | http.SetCookie(w, &http.Cookie{Name: "fruit", Value: "banana", Expires: time.Date(2000, time.January, 0, 0, 0, 0, 0, time.UTC)}) |
| 59 | for _, c := range r.Cookies() { |
| 60 | http.SetCookie(w, c) |
| 61 | } |
| 62 | |
| 63 | w.WriteHeader(http.StatusTeapot) |
| 64 | encoder := json.NewEncoder(w) |
| 65 | _ = encoder.Encode(struct{ RequestQueryParams, Method any }{r.URL.Query(), r.Method}) |
| 66 | }, |
| 67 | expectStatus: http.StatusTeapot, |
| 68 | expectHeaders: map[string]string{"Hello": "world1,world2"}, |
| 69 | expectCookies: []string{ |
| 70 | "yummy=cookie", |
| 71 | "yummy=cake", |
| 72 | "fruit=banana; Expires=Fri, 31 Dec 1999 00:00:00 GMT", |
| 73 | "foo=bar", |
| 74 | "hello=hello", |
| 75 | }, |
| 76 | expectBody: `{"RequestQueryParams":{"foo":["bar"],"hello":["world"]},"Method":"POST"}` + "\n", |
| 77 | }, |
| 78 | "mux": { |
| 79 | input: helloRequest, |
| 80 | handler: func(w http.ResponseWriter, r *http.Request) { |
| 81 | log.Println(r.URL) |
| 82 | mux := http.NewServeMux() |
| 83 | mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) { |
| 84 | w.WriteHeader(200) |
| 85 | _, _ = w.Write([]byte("Hello World!")) |
| 86 | }) |
| 87 | mux.ServeHTTP(w, r) |
| 88 | }, |
| 89 | expectStatus: 200, |
| 90 | expectBody: "Hello World!", |
| 91 | }, |
| 92 | "get-implicit-trailing-slash": { |
| 93 | input: domainOnlyGetRequest, |
| 94 | handler: func(w http.ResponseWriter, r *http.Request) { |
| 95 | encoder := json.NewEncoder(w) |
| 96 | _ = encoder.Encode(r.Method) |
| 97 | _ = encoder.Encode(r.URL.String()) |
| 98 | }, |