diff --git a/api.go b/api.go index 2b96d023c..bdaa35b03 100644 --- a/api.go +++ b/api.go @@ -213,7 +213,7 @@ func (api *API) CreateField(ctx context.Context, indexName string, fieldName str for _, opt := range opts { err := opt(&fo) if err != nil { - return nil, errors.Wrap(err, "applying option") + return nil, NewBadRequestError(errors.Wrap(err, "applying option")) } } diff --git a/http/handler.go b/http/handler.go index 1334af4d5..0d52a270b 100644 --- a/http/handler.go +++ b/http/handler.go @@ -782,6 +782,12 @@ func (h *Handler) handlePostField(w http.ResponseWriter, r *http.Request) { } _, err = h.api.CreateField(r.Context(), indexName, fieldName, fos...) + if err != nil { + if _, ok := err.(pilosa.BadRequestError); ok { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + } resp.write(w, err) } diff --git a/server/handler_test.go b/server/handler_test.go index fc832269a..dc4f571d5 100644 --- a/server/handler_test.go +++ b/server/handler_test.go @@ -22,6 +22,7 @@ import ( "fmt" "io" "io/ioutil" + "math" gohttp "net/http" "net/http/httptest" "reflect" @@ -542,6 +543,104 @@ func TestHandler_Endpoints(t *testing.T) { } }) + t.Run("Query int field unbounded", func(t *testing.T) { + w := httptest.NewRecorder() + fieldName := "f-int-ubound" + h.ServeHTTP(w, test.MustNewHTTPRequest("POST", fmt.Sprintf("/index/i0/field/%s", fieldName), + strings.NewReader(`{"options":{"type":"int"}}`))) + if w.Code != gohttp.StatusOK { + t.Fatalf("unexpected status code: %d", w.Code) + } + w = httptest.NewRecorder() + h.ServeHTTP(w, test.MustNewHTTPRequest("GET", "/schema", strings.NewReader(""))) + if w.Code != gohttp.StatusOK { + t.Fatalf("unexpected status code: %d", w.Code) + } + rsp := getSchemaResponse{} + if err := json.Unmarshal([]byte(w.Body.String()), &rsp); err != nil { + t.Fatalf("json decode: %s", err) + } + field := rsp.findField("i0", fieldName) + if field == nil { + t.Fatalf("field not found: %s", fieldName) + } + if math.MinInt64 != field.Options.Min { + t.Fatalf("field min %d != %d", math.MinInt64, field.Options.Min) + } + if math.MaxInt64 != field.Options.Max { + t.Fatalf("field max %d != %d", math.MaxInt64, field.Options.Max) + } + }) + + t.Run("Query int field unbounded min", func(t *testing.T) { + w := httptest.NewRecorder() + fieldName := "f-int-ubound-min" + h.ServeHTTP(w, test.MustNewHTTPRequest("POST", fmt.Sprintf("/index/i0/field/%s", fieldName), + strings.NewReader(`{"options":{"type":"int", "max": 10}}`))) + if w.Code != gohttp.StatusOK { + t.Fatalf("unexpected status code: %d", w.Code) + } + w = httptest.NewRecorder() + h.ServeHTTP(w, test.MustNewHTTPRequest("GET", "/schema", strings.NewReader(""))) + if w.Code != gohttp.StatusOK { + t.Fatalf("unexpected status code: %d", w.Code) + } + rsp := getSchemaResponse{} + if err := json.Unmarshal([]byte(w.Body.String()), &rsp); err != nil { + t.Fatalf("json decode: %s", err) + } + field := rsp.findField("i0", fieldName) + if field == nil { + t.Fatalf("field not found: %s", fieldName) + } + if math.MinInt64 != field.Options.Min { + t.Fatalf("field min %d != %d", math.MinInt64, field.Options.Min) + } + if 10 != field.Options.Max { + t.Fatalf("field max %d != %d", 10, field.Options.Max) + } + }) + + t.Run("Query int field unbounded max", func(t *testing.T) { + w := httptest.NewRecorder() + fieldName := "f-int-ubound-max" + h.ServeHTTP(w, test.MustNewHTTPRequest("POST", fmt.Sprintf("/index/i0/field/%s", fieldName), + strings.NewReader(`{"options":{"type":"int", "min": -10}}`))) + if w.Code != gohttp.StatusOK { + t.Fatalf("unexpected status code: %d", w.Code) + } + w = httptest.NewRecorder() + h.ServeHTTP(w, test.MustNewHTTPRequest("GET", "/schema", strings.NewReader(""))) + if w.Code != gohttp.StatusOK { + t.Fatalf("unexpected status code: %d", w.Code) + } + rsp := getSchemaResponse{} + if err := json.Unmarshal([]byte(w.Body.String()), &rsp); err != nil { + t.Fatalf("json decode: %s", err) + } + field := rsp.findField("i0", fieldName) + if field == nil { + t.Fatalf("field not found: %s", fieldName) + } + if -10 != field.Options.Min { + t.Fatalf("field min %d != %d", 10, field.Options.Min) + } + if math.MaxInt64 != field.Options.Max { + t.Fatalf("field max %d != %d", math.MaxInt64, field.Options.Max) + } + }) + + t.Run("Query int field min > max return 400", func(t *testing.T) { + w := httptest.NewRecorder() + fieldName := "f-int-ubound-err" + h.ServeHTTP(w, test.MustNewHTTPRequest("POST", fmt.Sprintf("/index/i0/field/%s", fieldName), + strings.NewReader(`{"options":{"type":"int", "min": 10, "max": -10}}`))) + fmt.Println("body", w.Body.String()) + if w.Code != gohttp.StatusBadRequest { + t.Fatalf("unexpected status code: %d", w.Code) + } + }) + t.Run("Method not allowed", func(t *testing.T) { w := httptest.NewRecorder() h.ServeHTTP(w, test.MustNewHTTPRequest("GET", "/index/i0/query", nil)) @@ -999,3 +1098,20 @@ func mustJSONDecodeSlice(t *testing.T, r io.Reader) (ret []interface{}) { } return ret } + +type getSchemaResponse struct { + Indexes []*pilosa.IndexInfo `json:"indexes"` +} + +func (r getSchemaResponse) findField(indexName, fieldName string) *pilosa.FieldInfo { + for _, index := range r.Indexes { + if index.Name == indexName { + for _, field := range index.Fields { + if field.Name == fieldName { + return field + } + } + } + } + return nil +}