|
4 | 4 | "bytes" |
5 | 5 | "io/ioutil" |
6 | 6 | "net/http" |
| 7 | + "strings" |
7 | 8 | "testing" |
8 | 9 |
|
9 | 10 | "github.com/cli/cli/v2/api" |
@@ -140,26 +141,67 @@ func runCommand(rt http.RoundTripper, remotes context.Remotes, isTTY bool, cli s |
140 | 141 | }, err |
141 | 142 | } |
142 | 143 |
|
143 | | -func TestPRDiff_notty(t *testing.T) { |
144 | | - http := &httpmock.Registry{} |
145 | | - defer http.Verify(t) |
| 144 | +func TestPRDiff_notty_diff(t *testing.T) { |
| 145 | + httpReg := &httpmock.Registry{} |
| 146 | + defer httpReg.Verify(t) |
146 | 147 |
|
147 | 148 | shared.RunCommandFinder("", &api.PullRequest{Number: 123}, ghrepo.New("OWNER", "REPO")) |
148 | 149 |
|
149 | | - http.Register( |
| 150 | + var gotAccept string |
| 151 | + httpReg.Register( |
| 152 | + httpmock.REST("GET", "repos/OWNER/REPO/pulls/123"), |
| 153 | + func(req *http.Request) (*http.Response, error) { |
| 154 | + gotAccept = req.Header.Get("Accept") |
| 155 | + return &http.Response{ |
| 156 | + StatusCode: 200, |
| 157 | + Request: req, |
| 158 | + Body: ioutil.NopCloser(strings.NewReader(testDiff)), |
| 159 | + }, nil |
| 160 | + }) |
| 161 | + |
| 162 | + output, err := runCommand(httpReg, nil, false, "") |
| 163 | + if err != nil { |
| 164 | + t.Fatalf("unexpected error: %s", err) |
| 165 | + } |
| 166 | + if diff := cmp.Diff(testDiff, output.String()); diff != "" { |
| 167 | + t.Errorf("command output did not match:\n%s", diff) |
| 168 | + } |
| 169 | + if gotAccept != "application/vnd.github.v3.diff" { |
| 170 | + t.Errorf("unexpected Accept header: %s", gotAccept) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +func TestPRDiff_notty_patch(t *testing.T) { |
| 175 | + httpReg := &httpmock.Registry{} |
| 176 | + defer httpReg.Verify(t) |
| 177 | + |
| 178 | + shared.RunCommandFinder("", &api.PullRequest{Number: 123}, ghrepo.New("OWNER", "REPO")) |
| 179 | + |
| 180 | + var gotAccept string |
| 181 | + httpReg.Register( |
150 | 182 | httpmock.REST("GET", "repos/OWNER/REPO/pulls/123"), |
151 | | - httpmock.StringResponse(testDiff)) |
| 183 | + func(req *http.Request) (*http.Response, error) { |
| 184 | + gotAccept = req.Header.Get("Accept") |
| 185 | + return &http.Response{ |
| 186 | + StatusCode: 200, |
| 187 | + Request: req, |
| 188 | + Body: ioutil.NopCloser(strings.NewReader(testDiff)), |
| 189 | + }, nil |
| 190 | + }) |
152 | 191 |
|
153 | | - output, err := runCommand(http, nil, false, "") |
| 192 | + output, err := runCommand(httpReg, nil, false, "--patch") |
154 | 193 | if err != nil { |
155 | 194 | t.Fatalf("unexpected error: %s", err) |
156 | 195 | } |
157 | 196 | if diff := cmp.Diff(testDiff, output.String()); diff != "" { |
158 | 197 | t.Errorf("command output did not match:\n%s", diff) |
159 | 198 | } |
| 199 | + if gotAccept != "application/vnd.github.v3.patch" { |
| 200 | + t.Errorf("unexpected Accept header: %s", gotAccept) |
| 201 | + } |
160 | 202 | } |
161 | 203 |
|
162 | | -func TestPRDiff_tty(t *testing.T) { |
| 204 | +func TestPRDiff_tty_diff(t *testing.T) { |
163 | 205 | http := &httpmock.Registry{} |
164 | 206 | defer http.Verify(t) |
165 | 207 |
|
|
0 commit comments