From 7825f015c07754906c4b6a5b462c1214b7e3f932 Mon Sep 17 00:00:00 2001 From: David Thorpe Date: Sat, 27 Jul 2024 13:12:42 +0200 Subject: [PATCH] Updated to include tests --- pkg/httpresponse/httpresponse_test.go | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/pkg/httpresponse/httpresponse_test.go b/pkg/httpresponse/httpresponse_test.go index 63402be..19e2dea 100644 --- a/pkg/httpresponse/httpresponse_test.go +++ b/pkg/httpresponse/httpresponse_test.go @@ -149,3 +149,39 @@ func Test_httpresponse_004(t *testing.T) { assert.Equal("{\n \"code\": 404,\n \"reason\": \"not found\",\n \"detail\": \"this is the detail\"\n}\n", resp.Body.String()) }) } + +func Test_httpresponse_005(t *testing.T) { + assert := assert.New(t) + + t.Run("Cors_0", func(t *testing.T) { + resp := httptest.NewRecorder() + assert.NoError(httpresponse.Cors(resp, "test")) + assert.Equal(200, resp.Code) + assert.Equal("test", resp.Header().Get("Access-Control-Allow-Origin")) + assert.Equal("*", resp.Header().Get("Access-Control-Allow-Methods")) + }) + + t.Run("Cors_1", func(t *testing.T) { + resp := httptest.NewRecorder() + assert.NoError(httpresponse.Cors(resp, "")) + assert.Equal(200, resp.Code) + assert.Equal("*", resp.Header().Get("Access-Control-Allow-Origin")) + assert.Equal("*", resp.Header().Get("Access-Control-Allow-Methods")) + }) + + t.Run("Cors_2", func(t *testing.T) { + resp := httptest.NewRecorder() + assert.NoError(httpresponse.Cors(resp, "", "get")) + assert.Equal(200, resp.Code) + assert.Equal("*", resp.Header().Get("Access-Control-Allow-Origin")) + assert.Equal("GET", resp.Header().Get("Access-Control-Allow-Methods")) + }) + + t.Run("Cors_3", func(t *testing.T) { + resp := httptest.NewRecorder() + assert.NoError(httpresponse.Cors(resp, "*", "get", "post")) + assert.Equal(200, resp.Code) + assert.Equal("*", resp.Header().Get("Access-Control-Allow-Origin")) + assert.Equal("GET,POST", resp.Header().Get("Access-Control-Allow-Methods")) + }) +}