|
| 1 | +package gorillamux_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/aws/aws-lambda-go/events" |
| 9 | + "github.com/awslabs/aws-lambda-go-api-proxy/gorillamux" |
| 10 | + "github.com/gorilla/mux" |
| 11 | + |
| 12 | + . "github.com/onsi/ginkgo" |
| 13 | + . "github.com/onsi/gomega" |
| 14 | +) |
| 15 | + |
| 16 | +var _ = Describe("GorillaMuxAdapterV2 tests", func() { |
| 17 | + Context("Simple ping request", func() { |
| 18 | + It("Proxies the event correctly", func() { |
| 19 | + homeHandler := func(w http.ResponseWriter, req *http.Request) { |
| 20 | + w.Header().Add("unfortunately-required-header", "") |
| 21 | + fmt.Fprintf(w, "Home Page") |
| 22 | + } |
| 23 | + |
| 24 | + productsHandler := func(w http.ResponseWriter, req *http.Request) { |
| 25 | + w.Header().Add("unfortunately-required-header", "") |
| 26 | + fmt.Fprintf(w, "Products Page") |
| 27 | + } |
| 28 | + |
| 29 | + r := mux.NewRouter() |
| 30 | + r.HandleFunc("/", homeHandler) |
| 31 | + r.HandleFunc("/products", productsHandler) |
| 32 | + |
| 33 | + adapter := gorillamux.NewV2(r) |
| 34 | + |
| 35 | + homePageReq := events.APIGatewayV2HTTPRequest{ |
| 36 | + RequestContext: events.APIGatewayV2HTTPRequestContext{ |
| 37 | + HTTP: events.APIGatewayV2HTTPRequestContextHTTPDescription{ |
| 38 | + Method: http.MethodGet, |
| 39 | + Path: "/", |
| 40 | + }, |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + homePageResp, homePageReqErr := adapter.ProxyWithContext(context.Background(), homePageReq) |
| 45 | + |
| 46 | + Expect(homePageReqErr).To(BeNil()) |
| 47 | + Expect(homePageResp.StatusCode).To(Equal(200)) |
| 48 | + Expect(homePageResp.Body).To(Equal("Home Page")) |
| 49 | + |
| 50 | + productsPageReq := events.APIGatewayV2HTTPRequest{ |
| 51 | + RequestContext: events.APIGatewayV2HTTPRequestContext{ |
| 52 | + HTTP: events.APIGatewayV2HTTPRequestContextHTTPDescription{ |
| 53 | + Method: http.MethodGet, |
| 54 | + Path: "/products", |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + productsPageResp, productsPageReqErr := adapter.Proxy(productsPageReq) |
| 60 | + |
| 61 | + Expect(productsPageReqErr).To(BeNil()) |
| 62 | + Expect(productsPageResp.StatusCode).To(Equal(200)) |
| 63 | + Expect(productsPageResp.Body).To(Equal("Products Page")) |
| 64 | + }) |
| 65 | + }) |
| 66 | +}) |
0 commit comments