Skip to content

Commit

Permalink
fix: add tests for allow CORS behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieugouel committed Dec 13, 2024
1 parent 61c7396 commit 818168b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
53 changes: 53 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,59 @@ func TestServe(t *testing.T) {
},
startHTTP,
},
{
"http allow CORS false",
"testdata/http.yml",
func(t *testing.T) {
req, err := http.NewRequest(http.MethodOptions, "http://127.0.0.1:9090?query=asd", nil)
checkErr(t, err)
resp, err := http.DefaultClient.Do(req)
checkErr(t, err)
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %d; expected: %d", resp.StatusCode, http.StatusOK)
}

resp.Body.Close()
checkHeader(t, resp, "Access-Control-Allow-Origin", "")
},
startHTTP,
},
{
"http allow CORS true without request Origin header",
"testdata/http.allow.cors.yml",
func(t *testing.T) {
q := "SELECT 123"
req, err := http.NewRequest("GET", "http://127.0.0.1:9090?query="+url.QueryEscape(q), nil)
checkErr(t, err)
resp, err := http.DefaultClient.Do(req)
checkErr(t, err)
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %d; expected: %d", resp.StatusCode, http.StatusOK)
}
resp.Body.Close()
checkHeader(t, resp, "Access-Control-Allow-Origin", "*")
},
startHTTP,
},
{
"http allow CORS true with request Origin header",
"testdata/http.allow.cors.yml",
func(t *testing.T) {
q := "SELECT 123"
req, err := http.NewRequest("GET", "http://127.0.0.1:9090?query="+url.QueryEscape(q), nil)
checkErr(t, err)
req.Header.Set("Origin", "http://example.com")
resp, err := http.DefaultClient.Do(req)
checkErr(t, err)
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %d; expected: %d", resp.StatusCode, http.StatusOK)
}

resp.Body.Close()
checkHeader(t, resp, "Access-Control-Allow-Origin", "http://example.com")
},
startHTTP,
},
}

// Wait until CHServer starts.
Expand Down
15 changes: 15 additions & 0 deletions testdata/http.allow.cors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
log_debug: true
server:
http:
listen_addr: ":9090"
allowed_networks: ["127.0.0.1/24"]

users:
- name: "default"
to_cluster: "default"
to_user: "default"
allow_cors: true

clusters:
- name: "default"
nodes: ["127.0.0.1:18124"]

0 comments on commit 818168b

Please sign in to comment.