schema_init_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package server
  15. import (
  16. "bytes"
  17. "net/http"
  18. "net/http/httptest"
  19. "testing"
  20. "github.com/gorilla/mux"
  21. "github.com/stretchr/testify/suite"
  22. )
  23. type SchemaTestSuite struct {
  24. suite.Suite
  25. sc schemaComp
  26. r *mux.Router
  27. }
  28. func (suite *SchemaTestSuite) SetupTest() {
  29. suite.sc = schemaComp{}
  30. suite.r = mux.NewRouter()
  31. suite.sc.register()
  32. suite.sc.rest(suite.r)
  33. }
  34. func (suite *SchemaTestSuite) TestSchema() {
  35. proto := `{"name": "test", "Content": "message ListOfDoubles {repeated double doubles=1;}"}`
  36. req, _ := http.NewRequest(http.MethodPost, "/schemas/protobuf", bytes.NewBufferString(proto))
  37. w := httptest.NewRecorder()
  38. suite.r.ServeHTTP(w, req)
  39. suite.Equal(http.StatusCreated, w.Code)
  40. req, _ = http.NewRequest(http.MethodGet, "/schemas/protobuf", bytes.NewBufferString("any"))
  41. w = httptest.NewRecorder()
  42. suite.r.ServeHTTP(w, req)
  43. suite.Equal(http.StatusOK, w.Code)
  44. req, _ = http.NewRequest(http.MethodGet, "/schemas/protobuf/test", bytes.NewBufferString("any"))
  45. w = httptest.NewRecorder()
  46. suite.r.ServeHTTP(w, req)
  47. suite.Equal(http.StatusOK, w.Code)
  48. req, _ = http.NewRequest(http.MethodDelete, "/schemas/protobuf/test", bytes.NewBufferString("any"))
  49. w = httptest.NewRecorder()
  50. suite.r.ServeHTTP(w, req)
  51. suite.Equal(http.StatusOK, w.Code)
  52. }
  53. func TestSchemaTestSuite(t *testing.T) {
  54. suite.Run(t, new(SchemaTestSuite))
  55. }