...
1
16
17 package aggregator
18
19 import (
20 "encoding/json"
21 "net/http"
22 "testing"
23
24 "github.com/stretchr/testify/assert"
25
26 "k8s.io/kube-openapi/pkg/handler3"
27 )
28
29 type handlerTest struct {
30 etag string
31 data []byte
32 }
33
34 var _ http.Handler = handlerTest{}
35
36 func (h handlerTest) ServeHTTP(w http.ResponseWriter, r *http.Request) {
37
38 if r.URL.Path == "/openapi/v3" {
39 group := &handler3.OpenAPIV3Discovery{
40 Paths: map[string]handler3.OpenAPIV3DiscoveryGroupVersion{
41 "apis/group/version": {
42 ServerRelativeURL: "/openapi/v3/apis/group/version?hash=" + h.etag,
43 },
44 },
45 }
46
47 j, _ := json.Marshal(group)
48 w.Write(j)
49 return
50 }
51
52 if r.URL.Path == "/openapi/v3/apis/group/version" {
53 if len(h.etag) > 0 {
54 w.Header().Add("Etag", h.etag)
55 }
56 ifNoneMatches := r.Header["If-None-Match"]
57 for _, match := range ifNoneMatches {
58 if match == h.etag {
59 w.WriteHeader(http.StatusNotModified)
60 return
61 }
62 }
63 w.Write(h.data)
64 }
65 }
66
67 func TestDownloadOpenAPISpec(t *testing.T) {
68 s := Downloader{}
69
70 groups, _, err := s.OpenAPIV3Root(
71 handlerTest{data: []byte(""), etag: ""})
72 assert.NoError(t, err)
73 if assert.NotNil(t, groups) {
74 assert.Equal(t, len(groups.Paths), 1)
75 if assert.Contains(t, groups.Paths, "apis/group/version") {
76 assert.NotEmpty(t, groups.Paths["apis/group/version"].ServerRelativeURL)
77 }
78 }
79
80 }
81
View as plain text