mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-10-09 04:17:51 +00:00
finish conversion of handler tests
This commit is contained in:
parent
8809751a23
commit
3c4ba82a4a
2 changed files with 30 additions and 36 deletions
|
|
@ -551,46 +551,33 @@ func TestHandler_Endpoints(t *testing.T) {
|
|||
}
|
||||
})
|
||||
|
||||
}
|
||||
t.Run("CORS", func(t *testing.T) {
|
||||
req := test.MustNewHTTPRequest("OPTIONS", "/index/foo/query", nil)
|
||||
req.Header.Add("Origin", "http://test/")
|
||||
req.Header.Add("Access-Control-Request-Method", "POST")
|
||||
|
||||
func TestHandler_CORS(t *testing.T) {
|
||||
t.Skip() // Until test.NewServer() works
|
||||
w := httptest.NewRecorder()
|
||||
h.ServeHTTP(w, req)
|
||||
result := w.Result()
|
||||
|
||||
hldr := test.MustOpenHolder()
|
||||
defer hldr.Close()
|
||||
// This handler does not support CORS, return Method Not Allowed (405)
|
||||
if result.StatusCode != 405 {
|
||||
t.Fatalf("CORS preflight status should be 405, but is %v", result.StatusCode)
|
||||
}
|
||||
|
||||
s := test.NewServer()
|
||||
s.Handler.API.Holder = hldr.Holder
|
||||
defer s.Close()
|
||||
clus := test.MustRunMainWithCluster(t, 1, test.OptAllowedOrigins([]string{"http://test/"}))
|
||||
w = httptest.NewRecorder()
|
||||
h := clus[0].Handler.(*http.Handler).Handler
|
||||
h.ServeHTTP(w, req)
|
||||
result = w.Result()
|
||||
|
||||
// No CORS config present, so should fail
|
||||
handler := test.MustNewHandler()
|
||||
|
||||
req := test.MustNewHTTPRequest("OPTIONS", "/index/foo/query", nil)
|
||||
req.Header.Add("Origin", "http://test/")
|
||||
req.Header.Add("Access-Control-Request-Method", "POST")
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
result := w.Result()
|
||||
|
||||
// This handler does not support CORS, return Method Not Allowed (405)
|
||||
if result.StatusCode != 405 {
|
||||
t.Fatalf("CORS preflight status should be 405, but is %v", result.StatusCode)
|
||||
}
|
||||
|
||||
// CORS config should allow preflight response
|
||||
handler = test.MustNewHandler(http.OptHandlerAllowedOrigins([]string{"http://test/"}))
|
||||
w = httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
result = w.Result()
|
||||
|
||||
if result.StatusCode != 200 {
|
||||
t.Fatalf("CORS preflight status should be 200, but is %v", result.StatusCode)
|
||||
}
|
||||
if w.HeaderMap["Access-Control-Allow-Origin"][0] != "http://test/" {
|
||||
t.Fatal("CORS header not present")
|
||||
}
|
||||
if result.StatusCode != 200 {
|
||||
t.Fatalf("CORS preflight status should be 200, but is %v", result.StatusCode)
|
||||
}
|
||||
if w.HeaderMap["Access-Control-Allow-Origin"][0] != "http://test/" {
|
||||
t.Fatal("CORS header not present")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func mustJSONDecode(t *testing.T, r io.Reader) (ret map[string]interface{}) {
|
||||
|
|
|
|||
|
|
@ -52,6 +52,13 @@ func OptAntiEntropyInterval(dur time.Duration) MainOpt {
|
|||
}
|
||||
}
|
||||
|
||||
func OptAllowedOrigins(origins []string) MainOpt {
|
||||
return func(m *Main) error {
|
||||
m.Config.Handler.AllowedOrigins = origins
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// NewMain returns a new instance of Main with a temporary data directory and random port.
|
||||
func NewMain(opts ...MainOpt) *Main {
|
||||
path, err := ioutil.TempDir("", "pilosa-")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue