From 60a69b90eb2b3313f9a458236f21f696e6f828f2 Mon Sep 17 00:00:00 2001 From: Nikhilesh Susarla Date: Tue, 16 Jul 2024 13:08:15 +0530 Subject: [PATCH 1/2] Update response.go Add CSS and JS content type --- response.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/response.go b/response.go index f819cd5..6e6d33c 100644 --- a/response.go +++ b/response.go @@ -51,6 +51,32 @@ func (ctx *RequestCtx) HTTPResponseBytes(body []byte, statusCode ...int) error { return nil } +// CSSResponseBytes returns response with body in css format. +func (ctx *RequestCtx) CSSResponseBytes(body []byte, statusCode ...int) error { + ctx.Response.Header.SetContentType("text/css; charset=utf-8") + + if len(statusCode) > 0 { + ctx.Response.Header.SetStatusCode(statusCode[0]) + } + + ctx.Response.SetBody(body) + + return nil +} + +// JSResponseBytes returns response with body in javascript format. +func (ctx *RequestCtx) JSResponseBytes(body []byte, statusCode ...int) error { + ctx.Response.Header.SetContentType("application/javascript") + + if len(statusCode) > 0 { + ctx.Response.Header.SetStatusCode(statusCode[0]) + } + + ctx.Response.SetBody(body) + + return nil +} + // TextResponse return response with body in text format. func (ctx *RequestCtx) TextResponse(body string, statusCode ...int) error { ctx.Response.Header.SetContentType("text/plain; charset=utf-8") From 59adc67e31e9ac0eb85c1feb89c7fa854a30511b Mon Sep 17 00:00:00 2001 From: Nikhilesh Susarla Date: Fri, 26 Jul 2024 22:33:40 +0530 Subject: [PATCH 2/2] Update response_test.go Add JSResponse test and CSSResponse test --- response_test.go | 212 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) diff --git a/response_test.go b/response_test.go index 09fa3dd..48ab554 100644 --- a/response_test.go +++ b/response_test.go @@ -432,6 +432,218 @@ func Test_ErrorResponse(t *testing.T) { } } +func TestCSSResponseBytes(t *testing.T) { //nolint:funlen + type args struct { + body []byte + statusCode int + } + + type want struct { + body string + statusCode int + contentType string + err bool + } + + tests := []struct { + name string + args args + want want + }{ + { + name: "ValidCSSBody", + args: args{ + body: []byte("body { background-color: #fff; }"), + statusCode: 200, + }, + want: want{ + body: "body { background-color: #fff; }", + statusCode: 200, + contentType: "text/css; charset=utf-8", + err: false, + }, + }, + { + name: "EmptyCSSBody", + args: args{ + body: []byte(""), + statusCode: 204, + }, + want: want{ + body: "", + statusCode: 204, + contentType: "text/css; charset=utf-8", + err: false, + }, + }, + { + name: "CustomCSSContent", + args: args{ + body: []byte(".container { display: flex; }"), + statusCode: 200, + }, + want: want{ + body: ".container { display: flex; }", + statusCode: 200, + contentType: "text/css; charset=utf-8", + err: false, + }, + }, + { + name: "InvalidCSSContent", + args: args{ + body: []byte("Invalid CSS content"), + statusCode: 200, + }, + want: want{ + body: "Invalid CSS content", + statusCode: 200, + contentType: "text/css; charset=utf-8", + err: false, // Assuming no error on invalid CSS content + }, + }, + } + + for _, test := range tests { + tt := test + + t.Run(tt.name, func(t *testing.T) { + t.Helper() + + ctx := new(fasthttp.RequestCtx) + actx := AcquireRequestCtx(ctx) + + err := actx.CSSResponseBytes(tt.args.body, tt.args.statusCode) + if tt.want.err && (err == nil) { + t.Errorf("CSSResponseBytes() Expected error") + } else if !tt.want.err && err != nil { + t.Errorf("CSSResponseBytes() Unexpected error: %v", err) + } + + responseBody := string(bytes.TrimSpace(actx.Response.Body())) + if responseBody != tt.want.body { + t.Errorf("body: '%v', want: '%v'", responseBody, tt.want.body) + } + + responseStatusCode := actx.Response.StatusCode() + if responseStatusCode != tt.want.statusCode { + t.Errorf("status_code: '%v', want: '%v'", responseStatusCode, tt.want.statusCode) + } + + responseContentType := string(actx.Response.Header.ContentType()) + if responseContentType != tt.want.contentType { + t.Errorf("content-type: '%v', want: '%v'", responseContentType, tt.want.contentType) + } + }) + } +} + +func TestJSResponseBytes(t *testing.T) { //nolint:funlen + type args struct { + body []byte + statusCode int + } + + type want struct { + body string + statusCode int + contentType string + err bool + } + + tests := []struct { + name string + args args + want want + }{ + { + name: "ValidBody", + args: args{ + body: []byte("var test = true;"), + statusCode: 200, + }, + want: want{ + body: "var test = true;", + statusCode: 200, + contentType: "application/javascript", + err: false, + }, + }, + { + name: "EmptyBody", + args: args{ + body: []byte(""), + statusCode: 204, + }, + want: want{ + body: "", + statusCode: 204, + contentType: "application/javascript", + err: false, + }, + }, + { + name: "CustomJavaScriptContent", + args: args{ + body: []byte("function test() { return true; }"), + statusCode: 200, + }, + want: want{ + body: "function test() { return true; }", + statusCode: 200, + contentType: "application/javascript", + err: false, + }, + }, + { + name: "InvalidBody", + args: args{ + body: []byte("Invalid JS content"), + statusCode: 200, + }, + want: want{ + body: "Invalid JS content", + statusCode: 200, + contentType: "application/javascript", + err: false, // Assuming no error on invalid JS content + }, + }, + } + + for _, test := range tests { + tt := test + + t.Run(tt.name, func(t *testing.T) { + t.Helper() + + ctx := new(fasthttp.RequestCtx) + actx := AcquireRequestCtx(ctx) + + err := actx.JSResponseBytes(tt.args.body, tt.args.statusCode) + if tt.want.err && (err == nil) { + t.Errorf("JSResponseBytes() Expected error") + } else if !tt.want.err && err != nil { + t.Errorf("JSResponseBytes() Unexpected error: %v", err) + } + + responseBody := string(bytes.TrimSpace(actx.Response.Body())) + if responseBody != tt.want.body { + t.Errorf("body: '%v', want: '%v'", responseBody, tt.want.body) + } + + responseStatusCode := actx.Response.StatusCode() + if responseStatusCode != tt.want.statusCode { + t.Errorf("status_code: '%v', want: '%v'", responseStatusCode, tt.want.statusCode) + } + + responseContentType := string(actx.Response.Header.ContentType()) + if responseContentType != tt.want.contentType { + t.Errorf("content-type: '%v', want: '%v'", responseContentType, tt.want.contentType) + } + }) + } +} + // Benchmarks. func Benchmark_FileResponse(b *testing.B) { cwd, _ := os.Getwd()