github.com/weaviate/weaviate@v1.24.6/modules/text2vec-contextionary/extensions/rest_user_facing_test.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package extensions 13 14 import ( 15 "bytes" 16 "context" 17 "io" 18 "net/http" 19 "net/http/httptest" 20 "testing" 21 22 "github.com/pkg/errors" 23 "github.com/stretchr/testify/assert" 24 "github.com/stretchr/testify/require" 25 "github.com/weaviate/weaviate/entities/models" 26 ) 27 28 func Test_UserFacingHandlers(t *testing.T) { 29 proxy := newFakeProxy() 30 h := NewRESTHandlers(nil, proxy) 31 32 t.Run("with a method other than POST", func(t *testing.T) { 33 r := httptest.NewRequest("GET", "/", nil) 34 w := httptest.NewRecorder() 35 h.UserFacingHandler().ServeHTTP(w, r) 36 37 res := w.Result() 38 defer res.Body.Close() 39 assert.Equal(t, http.StatusMethodNotAllowed, res.StatusCode) 40 }) 41 42 t.Run("with the wrong media type", func(t *testing.T) { 43 r := httptest.NewRequest("POST", "/", nil) 44 r.Header.Add("content-type", "text/plain") 45 w := httptest.NewRecorder() 46 h.UserFacingHandler().ServeHTTP(w, r) 47 48 res := w.Result() 49 defer res.Body.Close() 50 assert.Equal(t, http.StatusUnsupportedMediaType, res.StatusCode) 51 }) 52 53 t.Run("with the wrong body", func(t *testing.T) { 54 body := []byte(`{"concept":7}`) 55 r := httptest.NewRequest("POST", "/", bytes.NewReader(body)) 56 r.Header.Add("content-type", "application/json") 57 w := httptest.NewRecorder() 58 h.UserFacingHandler().ServeHTTP(w, r) 59 60 res := w.Result() 61 defer res.Body.Close() 62 assert.Equal(t, http.StatusUnprocessableEntity, res.StatusCode) 63 }) 64 65 t.Run("with the right body", func(t *testing.T) { 66 body := []byte(`{"concept":"foo","definition":"bar","weight":1}`) 67 r := httptest.NewRequest("POST", "/", bytes.NewReader(body)) 68 r.Header.Add("content-type", "application/json") 69 w := httptest.NewRecorder() 70 h.UserFacingHandler().ServeHTTP(w, r) 71 72 res := w.Result() 73 defer res.Body.Close() 74 75 readBody, err := io.ReadAll(res.Body) 76 require.Nil(t, err) 77 assert.Equal(t, http.StatusOK, res.StatusCode) 78 assert.Equal(t, body, readBody) 79 }) 80 81 t.Run("with a proxy error", func(t *testing.T) { 82 proxy.err = errors.Errorf("invalid input") 83 body := []byte(`{"concept":"foo","definition":"bar","weight":1}`) 84 r := httptest.NewRequest("POST", "/", bytes.NewReader(body)) 85 r.Header.Add("content-type", "application/json") 86 w := httptest.NewRecorder() 87 h.UserFacingHandler().ServeHTTP(w, r) 88 89 res := w.Result() 90 defer res.Body.Close() 91 assert.Equal(t, http.StatusBadRequest, res.StatusCode) 92 }) 93 } 94 95 type fakeProxy struct { 96 err error 97 } 98 99 func (f *fakeProxy) AddExtension(ctx context.Context, 100 ext *models.C11yExtension, 101 ) error { 102 return f.err 103 } 104 105 func newFakeProxy() *fakeProxy { 106 return &fakeProxy{} 107 }