From ecde8a8c8515eb17fc4b8a1aafec135e08d00bba Mon Sep 17 00:00:00 2001 From: ICHINOSE Shogo Date: Thu, 29 Dec 2022 02:54:17 +0900 Subject: [PATCH] add tests for lambdaResponseV2 --- ridgenative_test.go | 162 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 161 insertions(+), 1 deletion(-) diff --git a/ridgenative_test.go b/ridgenative_test.go index 6da2901..6b9d65d 100644 --- a/ridgenative_test.go +++ b/ridgenative_test.go @@ -401,14 +401,21 @@ func TestHTTPRequest(t *testing.T) { }) } -func TestResponse(t *testing.T) { +func TestResponseV1(t *testing.T) { l := &lambdaFunction{} t.Run("normal", func(t *testing.T) { rw := l.newResponseWriter() + // normal header fields rw.Header().Add("foo", "foo") + + // multi line header fields rw.Header().Add("bar", "bar1") rw.Header().Add("bar", "bar2") + // cookie + rw.Header().Add("Set-Cookie", "foo1=bar1") + rw.Header().Add("Set-Cookie", "foo2=bar2") + if _, err := io.WriteString(rw, "\n"); err != nil { t.Error(err) } @@ -426,6 +433,9 @@ func TestResponse(t *testing.T) { if resp.Headers["Bar"] != "bar1, bar2" { t.Errorf("unexpected header: want %q, got %q", "bar1, bar2", resp.Headers["Bar"]) } + if resp.Headers["Set-Cookie"] != "foo1=bar1" { + t.Errorf("unexpected header: want %q, got %q", "bar1, bar2", resp.Headers["Bar"]) + } if !reflect.DeepEqual(resp.MultiValueHeaders["Foo"], []string{"foo"}) { t.Errorf("unexpected header: want %#v, got %#v", []string{"foo"}, resp.MultiValueHeaders["Foo"]) } @@ -541,6 +551,156 @@ func TestResponse(t *testing.T) { }) } +func TestResponseV2(t *testing.T) { + l := &lambdaFunction{} + t.Run("normal", func(t *testing.T) { + rw := l.newResponseWriter() + + // normal header fields + rw.Header().Add("foo", "foo") + + // multi line header fields + rw.Header().Add("bar", "bar1") + rw.Header().Add("bar", "bar2") + + // cookie + rw.Header().Add("Set-Cookie", "foo1=bar1") + rw.Header().Add("Set-Cookie", "foo2=bar2") + + if _, err := io.WriteString(rw, "\n"); err != nil { + t.Error(err) + } + if _, err := rw.Write([]byte("Hello!")); err != nil { + t.Error(err) + } + + resp, err := rw.lambdaResponseV2() + if err != nil { + t.Error(err) + } + + // test headers + if resp.Headers["Foo"] != "foo" { + t.Errorf("unexpected header: want %q, got %q", "foo", resp.Headers["Foo"]) + } + if resp.Headers["Bar"] != "bar1, bar2" { + t.Errorf("unexpected header: want %q, got %q", "bar1, bar2", resp.Headers["Bar"]) + } + if v, ok := resp.Headers["Set-Cookie"]; ok { + t.Errorf("unexpected header: want None, got %q", v) + } + if got, want := resp.Cookies, []string{"foo1=bar1", "foo2=bar2"}; !reflect.DeepEqual(got, want) { + t.Errorf("unexpected cookie: want %#v, got %#v", want, got) + } + + // Content-Type is auto detected. + if resp.Headers["Content-Type"] != "text/html; charset=utf-8" { + t.Errorf("unexpected header: want %q, got %q", "text/html; charset=utf-8", resp.Headers["Content-Type"]) + } + + if resp.StatusCode != http.StatusOK { + t.Errorf("unexpected status code: want %d, got %d", http.StatusOK, resp.StatusCode) + } + if resp.Body != "\nHello!" { + t.Errorf("unexpected body: want %q, got %q", "\nHello!", resp.Body) + } + if resp.IsBase64Encoded { + t.Error("unexpected IsBase64Encoded: want false, got true") + } + }) + t.Run("set content-type", func(t *testing.T) { + rw := l.newResponseWriter() + rw.Header().Set("Content-Type", "text/plain; charset=utf-8") + if _, err := io.WriteString(rw, "\n"); err != nil { + t.Error(err) + } + if _, err := rw.Write([]byte("Hello!")); err != nil { + t.Error(err) + } + + resp, err := rw.lambdaResponseV1() + if err != nil { + t.Error(err) + } + + // Content-Type is auto detected. + if resp.Headers["Content-Type"] != "text/plain; charset=utf-8" { + t.Errorf("unexpected header: want %q, got %q", "text/plain; charset=utf-8", resp.Headers["Content-Type"]) + } + + if resp.StatusCode != http.StatusOK { + t.Errorf("unexpected status code: want %d, got %d", http.StatusOK, resp.StatusCode) + } + if resp.Body != "\nHello!" { + t.Errorf("unexpected body: want %q, got %q", "\nHello!", resp.Body) + } + if resp.IsBase64Encoded { + t.Error("unexpected IsBase64Encoded: want false, got true") + } + }) + t.Run("redirect to example.com", func(t *testing.T) { + rw := l.newResponseWriter() + rw.Header().Add("location", "http://example.com/") + rw.WriteHeader(http.StatusFound) + if _, err := io.WriteString(rw, "\n"); err != nil { + t.Error(err) + } + if _, err := rw.Write([]byte("Redirect to example.com")); err != nil { + t.Error(err) + } + + resp, err := rw.lambdaResponseV1() + if err != nil { + t.Error(err) + } + if resp.Headers["Location"] != "http://example.com/" { + t.Errorf("unexpected header: want %q, got %q", "http://example.com/", resp.Headers["Foo"]) + } + if resp.StatusCode != http.StatusFound { + t.Errorf("unexpected status code: want %d, got %d", http.StatusFound, resp.StatusCode) + } + if resp.Body != "\nRedirect to example.com" { + t.Errorf("unexpected body: want %q, got %q", "\nRedirect to example.com", resp.Body) + } + if resp.IsBase64Encoded { + t.Error("unexpected IsBase64Encoded: want false, got true") + } + }) + t.Run("base64", func(t *testing.T) { + rw := l.newResponseWriter() + // 1x1 PNG image + if _, err := io.WriteString(rw, "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"); err != nil { + t.Error(err) + } + if _, err := io.WriteString(rw, "\x00\x00\x00\x01\x00\x00\x00\x01\x08\x04\x00\x00\x00\xb5\x1c\x0c"); err != nil { + t.Error(err) + } + if _, err := io.WriteString(rw, "\x02\x00\x00\x00\x0b\x49\x44\x41\x54\x08\xd7\x63\x60\x60\x00\x00"); err != nil { + t.Error(err) + } + if _, err := io.WriteString(rw, "\x00\x03\x00\x01\x20\xd5\x94\xc7\x00\x00\x00\x00\x49\x45\x4e\x44"); err != nil { + t.Error(err) + } + if _, err := io.WriteString(rw, "\xae\x42\x60\x82"); err != nil { + t.Error(err) + } + + resp, err := rw.lambdaResponseV1() + if err != nil { + t.Error(err) + } + if resp.StatusCode != http.StatusOK { + t.Errorf("unexpected status code: want %d, got %d", http.StatusOK, resp.StatusCode) + } + if resp.Body != "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQI12NgYAAAAAMAASDVlMcAAAAASUVORK5CYII=" { + t.Errorf("unexpected body: want %q, got %q", "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQI12NgYAAAAAMAASDVlMcAAAAASUVORK5CYII=", resp.Body) + } + if !resp.IsBase64Encoded { + t.Error("unexpected IsBase64Encoded: want true, got false") + } + }) +} + func BenchmarkRequest_binary(b *testing.B) { l := newLambdaFunction(nil) req, err := loadRequest("testdata/apigateway-base64-request.json")