From be11f98463d947a6d0401e7d45d5e1efe4d62c5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20Lube=20de=20Bragan=C3=A7a?= Date: Wed, 6 Nov 2024 12:55:37 -0300 Subject: [PATCH 1/2] also add cors header in GET requests --- pkg/server.go | 3 +++ pkg/server_test.go | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/pkg/server.go b/pkg/server.go index f4f2132..e9ce827 100644 --- a/pkg/server.go +++ b/pkg/server.go @@ -313,6 +313,9 @@ func (s *Server) handleGet(w http.ResponseWriter, r *http.Request) (int, any) { } log.Printf("GET %s -> %s", r.URL.Path, requestPath) f, err := s.fs.Open(requestPath) + if s.EnableCORS { + w.Header().Set("Access-Control-Allow-Origin", "*") + } if err != nil { // ErrNotExist is a common case so don't log it if errors.Is(err, os.ErrNotExist) { diff --git a/pkg/server_test.go b/pkg/server_test.go index 18e0db6..3ee7b2d 100644 --- a/pkg/server_test.go +++ b/pkg/server_test.go @@ -25,6 +25,7 @@ func TestGetHandler(t *testing.T) { args args want int body string + headers map[string]string }{ { name: "get existing file", @@ -33,6 +34,9 @@ func TestGetHandler(t *testing.T) { Url: "/files/foo/bar.txt", }, want: http.StatusOK, + headers: map[string]string{ + "Access-Control-Allow-Origin": "*", + }, body: "hello, world", }, { @@ -42,6 +46,9 @@ func TestGetHandler(t *testing.T) { Url: "/files/bar/baz", }, want: http.StatusNotFound, + headers: map[string]string{ + "Access-Control-Allow-Origin": "*", + }, body: `{"ok":false,"error":"file not found"}`, }, { @@ -60,6 +67,9 @@ func TestGetHandler(t *testing.T) { Url: "/files/foo", }, want: http.StatusNotFound, + headers: map[string]string{ + "Access-Control-Allow-Origin": "*", + }, body: `{"ok":false,"error":"foo is a directory"}`, }, } @@ -93,6 +103,11 @@ func TestGetHandler(t *testing.T) { if body := rr.Body.String(); body != tt.body { t.Errorf("body = \"%s\", want = \"%s\"", body, tt.body) } + for k, v := range tt.headers { + if rr.Header().Get(k) != v { + t.Errorf("header %s = %s, want %s", k, rr.Header().Get(k), v) + } + } }) } } From dbd70d281adada23d95b95c2f03782f93add4526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20Lube=20de=20Bragan=C3=A7a?= Date: Wed, 6 Nov 2024 12:58:31 -0300 Subject: [PATCH 2/2] improve cors test coverage on post and put requests --- pkg/server_test.go | 51 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/pkg/server_test.go b/pkg/server_test.go index 3ee7b2d..4e4aa2a 100644 --- a/pkg/server_test.go +++ b/pkg/server_test.go @@ -21,10 +21,10 @@ func TestGetHandler(t *testing.T) { Url string } tests := []struct { - name string - args args - want int - body string + name string + args args + want int + body string headers map[string]string }{ { @@ -121,10 +121,11 @@ func TestServer_PostHandler(t *testing.T) { Name string } tests := []struct { - name string - args args - want int - body string + name string + args args + want int + body string + headers map[string]string }{ { name: "Post hello.txt", @@ -135,6 +136,9 @@ func TestServer_PostHandler(t *testing.T) { Name: "hello.txt", }, want: http.StatusCreated, + headers: map[string]string{ + "Access-Control-Allow-Origin": "*", + }, body: `{"ok":true,"path":"/files/hello.txt"}`, }, { @@ -146,6 +150,9 @@ func TestServer_PostHandler(t *testing.T) { Name: "empty", }, want: http.StatusCreated, + headers: map[string]string{ + "Access-Control-Allow-Origin": "*", + }, body: `{"ok":true,"path":"/files/empty"}`, }, { @@ -256,6 +263,11 @@ func TestServer_PostHandler(t *testing.T) { t.Errorf("failed to verify. request body = %v, local file = %v", tt.args.Content, uploaded) } } + for k, v := range tt.headers { + if rr.Header().Get(k) != v { + t.Errorf("header %s = %s, want %s", k, rr.Header().Get(k), v) + } + } }) } } @@ -269,10 +281,11 @@ func TestServer_PutHandler(t *testing.T) { Name string } tests := []struct { - name string - args args - want int - body string + name string + args args + want int + body string + headers map[string]string }{ { name: "PUT /files/hello.txt with text", @@ -283,6 +296,9 @@ func TestServer_PutHandler(t *testing.T) { Name: "hello.txt", }, want: http.StatusCreated, + headers: map[string]string{ + "Access-Control-Allow-Origin": "*", + }, body: `{"ok":true,"path":"/files/hello.txt"}`, }, { @@ -294,6 +310,9 @@ func TestServer_PutHandler(t *testing.T) { Name: "empty", }, want: http.StatusCreated, + headers: map[string]string{ + "Access-Control-Allow-Origin": "*", + }, body: `{"ok":true,"path":"/files/empty"}`, }, { @@ -305,6 +324,9 @@ func TestServer_PutHandler(t *testing.T) { Name: "world.txt", }, want: http.StatusCreated, + headers: map[string]string{ + "Access-Control-Allow-Origin": "*", + }, body: `{"ok":true,"path":"/files/hello/world.txt"}`, }, { @@ -401,6 +423,11 @@ func TestServer_PutHandler(t *testing.T) { t.Errorf("failed to verify. request body = %v, local file = %v", tt.args.Content, uploaded) } } + for k, v := range tt.headers { + if rr.Header().Get(k) != v { + t.Errorf("header %s = %s, want %s", k, rr.Header().Get(k), v) + } + } }) } }