Skip to content

Commit 084bb5c

Browse files
Add CORS policy for /v2beta1/query handler (#16)
* Return http.Response from UnmarshalResponse * Set wildcard CORS policy * Set Content-Type to application/json
1 parent 3e56356 commit 084bb5c

File tree

5 files changed

+25
-11
lines changed

5 files changed

+25
-11
lines changed

cmd/monitoring-token/monitoring-token.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func main() {
8585

8686
// Get monitoring result.
8787
mr := &v2.MonitoringResult{}
88-
err = proxy.UnmarshalResponse(req, mr)
88+
_, err = proxy.UnmarshalResponse(req, mr)
8989
rtx.Must(err, "Failed to get response")
9090
logx.Debug.Println(pretty.Sprint(mr))
9191
if mr.Error != nil {

handler/handler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ func (c *Client) TranslatedQuery(rw http.ResponseWriter, req *http.Request) {
6262
result := v2.QueryResult{}
6363
experiment, service := getExperimentAndService(req.URL.Path)
6464

65+
// Set CORS policy to allow third-party websites to use returned resources.
66+
rw.Header().Set("Content-Type", "application/json")
67+
rw.Header().Set("Access-Control-Allow-Origin", "*")
68+
6569
// Check whether the service is valid before all other steps to fail fast.
6670
ports, ok := static.Configs[service]
6771
if !ok {

handler/handler_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,18 @@ func TestClient_TranslatedQuery(t *testing.T) {
9494
req.Header.Set("X-AppEngine-CityLatLong", tt.latlon)
9595

9696
result := &v2.QueryResult{}
97-
err = proxy.UnmarshalResponse(req, result)
97+
resp, err := proxy.UnmarshalResponse(req, result)
9898
if err != nil {
9999
t.Fatalf("Failed to get response from: %s %s", srv.URL, tt.path)
100100
}
101-
101+
if resp.Header.Get("Access-Control-Allow-Origin") != "*" {
102+
t.Errorf("TranslatedQuery() wrong Access-Control-Allow-Origin header; got %s, want '*'",
103+
resp.Header.Get("Access-Control-Allow-Origin"))
104+
}
105+
if resp.Header.Get("Content-Type") != "application/json" {
106+
t.Errorf("TranslatedQuery() wrong Content-Type header; got %s, want 'application/json'",
107+
resp.Header.Get("Content-Type"))
108+
}
102109
if result.Error != nil && result.Error.Status != tt.wantStatus {
103110
t.Errorf("TranslatedQuery() wrong status; got %d, want %d", result.Error.Status, tt.wantStatus)
104111
}

proxy/proxy.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (ll *LegacyLocator) Nearest(ctx context.Context, service, lat, lon string)
7272
return nil, err
7373
}
7474
opts := &geoOptions{}
75-
err = UnmarshalResponse(req, opts)
75+
_, err = UnmarshalResponse(req, opts)
7676
if err != nil {
7777
return nil, err
7878
}
@@ -81,21 +81,21 @@ func (ll *LegacyLocator) Nearest(ctx context.Context, service, lat, lon string)
8181

8282
// UnmarshalResponse reads the response from the given request and unmarshals
8383
// the value into the given result.
84-
func UnmarshalResponse(req *http.Request, result interface{}) error {
84+
func UnmarshalResponse(req *http.Request, result interface{}) (*http.Response, error) {
8585
resp, err := http.DefaultClient.Do(req)
8686
if err != nil {
87-
return err
87+
return resp, err
8888
}
8989
if resp.StatusCode == http.StatusNoContent {
9090
// Cannot unmarshal empty content.
91-
return ErrNoContent
91+
return resp, ErrNoContent
9292
}
9393
defer resp.Body.Close()
9494
b, err := ioutil.ReadAll(resp.Body)
9595
if err != nil {
96-
return err
96+
return resp, err
9797
}
98-
return json.Unmarshal(b, result)
98+
return resp, json.Unmarshal(b, result)
9999
}
100100

101101
// collect reads all FQDN results from the given options and guarantees to

proxy/proxy_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,16 @@ func Test_UnmarshalResponse(t *testing.T) {
303303
if err != nil {
304304
t.Errorf("failed to create request")
305305
}
306-
if err := UnmarshalResponse(req, tt.result); (err != nil) != tt.wantErr {
306+
resp, err := UnmarshalResponse(req, tt.result)
307+
if (err != nil) != tt.wantErr {
307308
t.Errorf("getRequest() error = %v, wantErr %v", err, tt.wantErr)
308309
}
309310
if tt.wantErr {
310311
return
311312
}
312-
313+
if resp.StatusCode != tt.status {
314+
t.Errorf("UnmarshalResponse() got %d, want %d", resp.StatusCode, tt.status)
315+
}
313316
obj := tt.result.(*fakeObject)
314317
if obj.Message != "success" {
315318
t.Errorf("Result did not decode message: got %q, want 'success'", obj.Message)

0 commit comments

Comments
 (0)