|
| 1 | +package core_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/rand" |
| 6 | + "encoding/base64" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/aws/aws-lambda-go/events" |
| 10 | + "github.com/awslabs/aws-lambda-go-api-proxy/core" |
| 11 | + . "github.com/onsi/ginkgo" |
| 12 | + . "github.com/onsi/gomega" |
| 13 | +) |
| 14 | + |
| 15 | +var _ = Describe("RequestAccessorFu tests", func() { |
| 16 | + Context("Function URL event conversion", func() { |
| 17 | + accessor := core.RequestAccessorFu{} |
| 18 | + qs := make(map[string]string) |
| 19 | + mvqs := make(map[string][]string) |
| 20 | + hdr := make(map[string]string) |
| 21 | + qs["UniqueId"] = "12345" |
| 22 | + hdr["header1"] = "Testhdr1" |
| 23 | + hdr["header2"] = "Testhdr2" |
| 24 | + // Multivalue query strings |
| 25 | + mvqs["k1"] = []string{"t1"} |
| 26 | + mvqs["k2"] = []string{"t2"} |
| 27 | + bdy := "Test BODY" |
| 28 | + basePathRequest := getFunctionUrlProxyRequest("/hello", getFunctionUrlRequestContext("/hello", "GET"), false, hdr, bdy, qs, mvqs) |
| 29 | + |
| 30 | + It("Correctly converts a basic event", func() { |
| 31 | + httpReq, err := accessor.EventToRequestWithContext(context.Background(), basePathRequest) |
| 32 | + Expect(err).To(BeNil()) |
| 33 | + Expect("/hello").To(Equal(httpReq.URL.Path)) |
| 34 | + Expect("/hello?UniqueId=12345").To(Equal(httpReq.RequestURI)) |
| 35 | + Expect("GET").To(Equal(httpReq.Method)) |
| 36 | + headers := basePathRequest.Headers |
| 37 | + Expect(2).To(Equal(len(headers))) |
| 38 | + }) |
| 39 | + |
| 40 | + binaryBody := make([]byte, 256) |
| 41 | + _, err := rand.Read(binaryBody) |
| 42 | + if err != nil { |
| 43 | + Fail("Could not generate random binary body") |
| 44 | + } |
| 45 | + |
| 46 | + encodedBody := base64.StdEncoding.EncodeToString(binaryBody) |
| 47 | + |
| 48 | + binaryRequest := getFunctionUrlProxyRequest("/hello", getFunctionUrlRequestContext("/hello", "POST"), true, hdr, bdy, qs, mvqs) |
| 49 | + binaryRequest.Body = encodedBody |
| 50 | + binaryRequest.IsBase64Encoded = true |
| 51 | + |
| 52 | + It("Decodes a base64 encoded body", func() { |
| 53 | + httpReq, err := accessor.EventToRequestWithContext(context.Background(), binaryRequest) |
| 54 | + Expect(err).To(BeNil()) |
| 55 | + Expect("/hello").To(Equal(httpReq.URL.Path)) |
| 56 | + Expect("/hello?UniqueId=12345").To(Equal(httpReq.RequestURI)) |
| 57 | + Expect("POST").To(Equal(httpReq.Method)) |
| 58 | + }) |
| 59 | + |
| 60 | + mqsRequest := getFunctionUrlProxyRequest("/hello", getFunctionUrlRequestContext("/hello", "GET"), false, hdr, bdy, qs, mvqs) |
| 61 | + mqsRequest.RawQueryString = "hello=1&world=2&world=3" |
| 62 | + mqsRequest.QueryStringParameters = map[string]string{ |
| 63 | + "hello": "1", |
| 64 | + "world": "2", |
| 65 | + } |
| 66 | + |
| 67 | + It("Populates query string correctly", func() { |
| 68 | + httpReq, err := accessor.EventToRequestWithContext(context.Background(), mqsRequest) |
| 69 | + Expect(err).To(BeNil()) |
| 70 | + Expect("/hello").To(Equal(httpReq.URL.Path)) |
| 71 | + fmt.Println("SDYFSDKFJDL") |
| 72 | + fmt.Printf("%v", httpReq.RequestURI) |
| 73 | + Expect(httpReq.RequestURI).To(ContainSubstring("hello=1")) |
| 74 | + Expect(httpReq.RequestURI).To(ContainSubstring("world=2")) |
| 75 | + Expect("GET").To(Equal(httpReq.Method)) |
| 76 | + query := httpReq.URL.Query() |
| 77 | + Expect(2).To(Equal(len(query))) |
| 78 | + Expect(query["hello"]).ToNot(BeNil()) |
| 79 | + Expect(query["world"]).ToNot(BeNil()) |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + Context("StripBasePath tests", func() { |
| 84 | + accessor := core.RequestAccessorFu{} |
| 85 | + It("Adds prefix slash", func() { |
| 86 | + basePath := accessor.StripBasePath("app1") |
| 87 | + Expect("/app1").To(Equal(basePath)) |
| 88 | + }) |
| 89 | + |
| 90 | + It("Removes trailing slash", func() { |
| 91 | + basePath := accessor.StripBasePath("/app1/") |
| 92 | + Expect("/app1").To(Equal(basePath)) |
| 93 | + }) |
| 94 | + |
| 95 | + It("Ignores blank strings", func() { |
| 96 | + basePath := accessor.StripBasePath(" ") |
| 97 | + Expect("").To(Equal(basePath)) |
| 98 | + }) |
| 99 | + }) |
| 100 | +}) |
| 101 | + |
| 102 | +func getFunctionUrlProxyRequest(path string, requestCtx events.LambdaFunctionURLRequestContext, |
| 103 | + is64 bool, header map[string]string, body string, qs map[string]string, mvqs map[string][]string) events.LambdaFunctionURLRequest { |
| 104 | + return events.LambdaFunctionURLRequest{ |
| 105 | + RequestContext: requestCtx, |
| 106 | + RawPath: path, |
| 107 | + RawQueryString: generateQueryString(qs), |
| 108 | + Headers: header, |
| 109 | + Body: body, |
| 110 | + IsBase64Encoded: is64, |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func getFunctionUrlRequestContext(path, method string) events.LambdaFunctionURLRequestContext { |
| 115 | + return events.LambdaFunctionURLRequestContext{ |
| 116 | + DomainName: "example.com", |
| 117 | + HTTP: events.LambdaFunctionURLRequestContextHTTPDescription{ |
| 118 | + Method: method, |
| 119 | + Path: path, |
| 120 | + }, |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func generateQueryString(queryParameters map[string]string) string { |
| 125 | + var queryString string |
| 126 | + for key, value := range queryParameters { |
| 127 | + if queryString != "" { |
| 128 | + queryString += "&" |
| 129 | + } |
| 130 | + queryString += fmt.Sprintf("%s=%s", key, value) |
| 131 | + } |
| 132 | + return queryString |
| 133 | +} |
0 commit comments