loading_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2015 go-swagger maintainers
  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 swag
  15. import (
  16. "net/http"
  17. "net/http/httptest"
  18. "testing"
  19. "github.com/stretchr/testify/assert"
  20. )
  21. func TestLoadFromHTTP(t *testing.T) {
  22. _, err := LoadFromFileOrHTTP("httx://12394:abd")
  23. assert.Error(t, err)
  24. serv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  25. rw.WriteHeader(http.StatusNotFound)
  26. }))
  27. defer serv.Close()
  28. _, err = LoadFromFileOrHTTP(serv.URL)
  29. assert.Error(t, err)
  30. ts2 := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  31. rw.WriteHeader(http.StatusOK)
  32. _, _ = rw.Write([]byte("the content"))
  33. }))
  34. defer ts2.Close()
  35. d, err := LoadFromFileOrHTTP(ts2.URL)
  36. assert.NoError(t, err)
  37. assert.Equal(t, []byte("the content"), d)
  38. }