MCPcopy Create free account
hub / github.com/cli/cli / TestMockServer

Function TestMockServer

script/ghmock/server_test.go:74–523  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

72}
73
74func TestMockServer(t *testing.T) {
75 limitBody := strings.Repeat("x", maxRequestBody)
76 rawBytes := []byte{0, 255, ' ', '\n'}
77 type call struct {
78 method string
79 url string
80 body string
81 }
82 type reply struct {
83 err bool
84 status int
85 body string
86 headers map[string]string
87 }
88
89 tests := []struct {
90 name string
91 responses responseSet
92 checkSchemas bool
93 // dotcom gives the server a certificate authority, as without
94 // --localhost, and the client trusts it. untrusted does the same, but
95 // the client trusts nothing.
96 dotcom bool
97 untrusted bool
98 calls []call
99 want []reply
100 wantLog []logSummary
101 wantKinds []string
102 // wantSent, when set, is the response body each logged request
103 // records as sent to gh.
104 wantSent []string
105 wantRawSent [][]byte
106 wantRawRequests [][]byte
107 exactBody bool
108 }{
109 {
110 name: "typename aliases trim fields from other concrete fragments",
111 responses: responseSet{"graphql Kind": {{Body: json.RawMessage(
112 `{"data":{"repository":{"issueOrPullRequest":{"kind":"Issue","title":"Bug","number":777}}}}`)}}},
113 checkSchemas: true,
114 calls: []call{{method: "POST", url: "http://api.github.localhost/graphql", body: graphQLBody(t,
115 `query Kind { repository(owner:"o",name:"r") { issueOrPullRequest(number:1) { kind:__typename ...on Issue { title } ...on PullRequest { number } } } }`)}},
116 want: []reply{{status: 200, body: `{"data":{"repository":{"issueOrPullRequest":{"kind":"Issue","title":"Bug"}}}}`}},
117 wantLog: []logSummary{{Seq: 1, Name: "graphql Kind", Status: 200, Matched: true, Response: 1, Responses: 1}},
118 wantSent: []string{`{"data":{"repository":{"issueOrPullRequest":{"kind":"Issue","title":"Bug"}}}}`},
119 },
120 {
121 name: "request at the body limit is recorded in full",
122 responses: responseSet{"POST /limit": {{Body: json.RawMessage(`{}`)}}},
123 calls: []call{{method: "POST", url: "http://api.github.localhost/limit", body: limitBody}},
124 want: []reply{{status: 200, body: `{}`}},
125 wantLog: []logSummary{{Seq: 1, Name: "POST /limit", Body: limitBody, Status: 200, Matched: true, Response: 1, Responses: 1}},
126 },
127 {
128 name: "oversized request is rejected before fixture matching",
129 responses: responseSet{"POST /limit": {{Body: json.RawMessage(`{}`)}}},
130 calls: []call{{method: "POST", url: "http://api.github.localhost/limit", body: limitBody + "x"}},
131 want: []reply{{status: 413, body: `{"message":"ghmock: request body exceeds 10 MiB"}`}},

Callers

nothing calls this directly

Calls 15

saveResponsesMethod · 0.95
appendLogMethod · 0.95
readLogMethod · 0.95
graphQLBodyFunction · 0.85
graphQLBodyWithVariablesFunction · 0.85
testCheckerFunction · 0.85
newCertAuthorityFunction · 0.85
startServerFunction · 0.85
summarizeFunction · 0.85
proxyURLMethod · 0.80
EqualMethod · 0.80
RunMethod · 0.65

Tested by

no test coverage detected