Skip to content

Commit

Permalink
add tests for lambdaResponseV2
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo82148 committed Dec 28, 2022
1 parent e51b905 commit ecde8a8
Showing 1 changed file with 161 additions and 1 deletion.
162 changes: 161 additions & 1 deletion ridgenative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<!DOCTYPE html>\n"); err != nil {
t.Error(err)
}
Expand All @@ -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"])
}
Expand Down Expand Up @@ -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, "<!DOCTYPE html>\n"); err != nil {
t.Error(err)
}
if _, err := rw.Write([]byte("<html><body>Hello!</body></html>")); 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 != "<!DOCTYPE html>\n<html><body>Hello!</body></html>" {
t.Errorf("unexpected body: want %q, got %q", "<!DOCTYPE html>\n<html><body>Hello!</body></html>", 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, "<!DOCTYPE html>\n"); err != nil {
t.Error(err)
}
if _, err := rw.Write([]byte("<html><body>Hello!</body></html>")); 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 != "<!DOCTYPE html>\n<html><body>Hello!</body></html>" {
t.Errorf("unexpected body: want %q, got %q", "<!DOCTYPE html>\n<html><body>Hello!</body></html>", 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, "<!DOCTYPE html>\n"); err != nil {
t.Error(err)
}
if _, err := rw.Write([]byte("<html><body>Redirect to <a href=http://example.com/>example.com</a></body></html>")); 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 != "<!DOCTYPE html>\n<html><body>Redirect to <a href=http://example.com/>example.com</a></body></html>" {
t.Errorf("unexpected body: want %q, got %q", "<!DOCTYPE html>\n<html><body>Redirect to <a href=http://example.com/>example.com</a></body></html>", 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")
Expand Down

0 comments on commit ecde8a8

Please sign in to comment.