Skip to content

Commit

Permalink
Merge pull request #46 from felipelube/feat/add-cors-header-on-get-re…
Browse files Browse the repository at this point in the history
…sponse

add cors header on get response
  • Loading branch information
mayth authored Nov 10, 2024
2 parents d0413a7 + dbd70d2 commit ac9dbf3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
3 changes: 3 additions & 0 deletions pkg/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
66 changes: 54 additions & 12 deletions pkg/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ 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
}{
{
name: "get existing file",
Expand All @@ -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",
},
{
Expand All @@ -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"}`,
},
{
Expand All @@ -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"}`,
},
}
Expand Down Expand Up @@ -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)
}
}
})
}
}
Expand All @@ -106,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",
Expand All @@ -120,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"}`,
},
{
Expand All @@ -131,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"}`,
},
{
Expand Down Expand Up @@ -241,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)
}
}
})
}
}
Expand All @@ -254,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",
Expand All @@ -268,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"}`,
},
{
Expand All @@ -279,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"}`,
},
{
Expand All @@ -290,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"}`,
},
{
Expand Down Expand Up @@ -386,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)
}
}
})
}
}
Expand Down

0 comments on commit ac9dbf3

Please sign in to comment.