File: json_test.go

package info (click to toggle)
golang-github-go-openapi-swag 1%3A0.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,200 kB
  • sloc: sh: 30; makefile: 3
file content (43 lines) | stat: -rw-r--r-- 1,063 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package loading

import (
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestJSONMatcher(t *testing.T) {
	t.Run("should recognize a json file", func(t *testing.T) {
		assert.True(t, JSONMatcher("local.json"))
		assert.True(t, JSONMatcher("local.jso"))
		assert.True(t, JSONMatcher("local.jsn"))
		assert.False(t, JSONMatcher("local.yml"))
	})
}

func TestJSONDoc(t *testing.T) {
	t.Run("should retrieve pet store API as JSON", func(t *testing.T) {
		serv := httptest.NewServer(http.HandlerFunc(serveJSONPestore))

		defer serv.Close()

		s, err := JSONDoc(serv.URL)
		require.NoError(t, err)
		require.NotNil(t, s)
		require.JSONEq(t, string(jsonPetStore), string(s))
	})

	t.Run("should not retrieve any doc", func(t *testing.T) {
		ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
			rw.WriteHeader(http.StatusNotFound)
			_, _ = rw.Write([]byte("\n"))
		}))
		defer ts.Close()

		_, err := JSONDoc(ts.URL)
		require.Error(t, err)
	})
}