From 8f702e41bec013f9d7d5fca24de0eba71a094bf6 Mon Sep 17 00:00:00 2001 From: ICHINOSE Shogo Date: Tue, 6 Feb 2024 16:14:05 +0900 Subject: [PATCH] check Content-Encoding header value --- ridgenative.go | 8 ++- ridgenative_test.go | 136 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 123 insertions(+), 21 deletions(-) diff --git a/ridgenative.go b/ridgenative.go index ca4bb9f..2cb5fce 100644 --- a/ridgenative.go +++ b/ridgenative.go @@ -360,7 +360,13 @@ func (rw *responseWriter) detectContentType() { } // assume text/*, application/json, application/javascript, application/xml, */*+json, */*+xml as text -func isBinary(contentType string) bool { +func isBinary(headers http.Header) bool { + contentEncoding := headers.Values("Content-Encoding") + if len(contentEncoding) > 0 { + return true + } + + contentType := headers.Get("Content-Type") i := strings.Index(contentType, ";") if i == -1 { i = len(contentType) diff --git a/ridgenative_test.go b/ridgenative_test.go index abacbb4..e59dd19 100644 --- a/ridgenative_test.go +++ b/ridgenative_test.go @@ -952,31 +952,127 @@ func TestLambdaHandlerStreaming(t *testing.T) { func TestIsBinary(t *testing.T) { tests := []struct { - contentType string - want bool + header http.Header + want bool }{ - {"text/html", false}, - {"text/plain", false}, - {"text/xml", false}, - {"application/json", false}, - {"application/javascript", false}, - {"application/yaml", false}, - {"application/xml", false}, - {"application/vnd.foo+json", false}, - {"application/vnd.foo+yaml", false}, - {"application/vnd.foo+xml", false}, - {"application/vnd.foo+xml ; charset=utf8", false}, - {"application/octet-stream", true}, - {"image/jpeg", true}, - {"audio/mpeg", true}, - {"unknown-content-type", true}, - {"", true}, + // text/html but encoded as gzip + { + header: http.Header{ + "Content-Type": []string{"text/html; charset=utf-8"}, + "Content-Encoding": []string{"gzip"}, + }, + want: true, + }, + + // text/* + { + header: http.Header{ + "Content-Type": []string{"text/html"}, + }, + want: false, + }, + { + header: http.Header{ + "Content-Type": []string{"text/plain"}, + }, + want: false, + }, + { + header: http.Header{ + "Content-Type": []string{"text/xml"}, + }, + want: false, + }, + + // data formats that are encoded as text + { + header: http.Header{ + "Content-Type": []string{"application/json"}, + }, + want: false, + }, + { + header: http.Header{ + "Content-Type": []string{"application/javascript"}, + }, + want: false, + }, + { + header: http.Header{ + "Content-Type": []string{"application/yaml"}, + }, + want: false, + }, + { + header: http.Header{ + "Content-Type": []string{"application/xml"}, + }, + want: false, + }, + + // custom media types that are encoded as text + { + header: http.Header{ + "Content-Type": []string{"application/foo+json"}, + }, + want: false, + }, + { + header: http.Header{ + "Content-Type": []string{"application/foo+yaml"}, + }, + want: false, + }, + { + header: http.Header{ + "Content-Type": []string{"application/foo+xml"}, + }, + want: false, + }, + { + header: http.Header{ + "Content-Type": []string{"application/foo+xml ; charset=utf8"}, + }, + want: false, + }, + + // common binary formats + { + header: http.Header{ + "Content-Type": []string{"application/octet-stream"}, + }, + want: true, + }, + { + header: http.Header{ + "Content-Type": []string{"image/jpeg"}, + }, + want: true, + }, + { + header: http.Header{ + "Content-Type": []string{"audio/mpeg"}, + }, + want: true, + }, + { + header: http.Header{ + "Content-Type": []string{"unknown-content-type"}, + }, + want: true, + }, + { + header: http.Header{ + "Content-Type": []string{""}, + }, + want: true, + }, } for _, tt := range tests { - got := isBinary(tt.contentType) + got := isBinary(tt.header) if got != tt.want { - t.Errorf("isBinary(%q) = %v, want %v", tt.contentType, got, tt.want) + t.Errorf("isBinary(%v) = %v, want %v", tt.header, got, tt.want) } } }