forked from target/pull-request-code-coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_test.go
More file actions
88 lines (64 loc) · 2.96 KB
/
Copy pathdiff_test.go
File metadata and controls
88 lines (64 loc) · 2.96 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package githubdiff
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/target/pull-request-code-coverage/internal/plugin/pluginhttp"
)
func TestLoader_Load_BuildsRequestAndReturnsDiff(t *testing.T) {
mockClient := &pluginhttp.MockClient{}
request := httptest.NewRequest("GET", "http://anywhere", nil)
mockClient.
On("NewRequest", "GET", "https://api.github.com/repos/some_org/some_repo/pulls/123", nil).
Return(request, nil)
mockClient.
On("Do", request).
Return(&http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader("THE DIFF"))}, nil)
reader, err := NewLoader("SOME_API_KEY", "https://api.github.com", "123", "some_org", "some_repo", mockClient).Load()
assert.NoError(t, err)
body, _ := io.ReadAll(reader)
assert.Equal(t, "THE DIFF", string(body))
assert.Equal(t, "token SOME_API_KEY", request.Header.Get("Authorization"))
assert.Equal(t, "application/vnd.github.v3.diff", request.Header.Get("Accept"))
mockClient.AssertExpectations(t)
}
func TestLoader_Load_TrimsTrailingSlashOnBaseURL(t *testing.T) {
mockClient := &pluginhttp.MockClient{}
request := httptest.NewRequest("GET", "http://anywhere", nil)
mockClient.
On("NewRequest", "GET", "https://git.example.com/api/v3/repos/o/r/pulls/9", nil).
Return(request, nil)
mockClient.
On("Do", request).
Return(&http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(""))}, nil)
_, err := NewLoader("k", "https://git.example.com/api/v3/", "9", "o", "r", mockClient).Load()
assert.NoError(t, err)
mockClient.AssertExpectations(t)
}
func TestLoader_Load_FailedNewRequest(t *testing.T) {
mockClient := &pluginhttp.MockClient{}
mockClient.On("NewRequest", mock.Anything, mock.Anything, mock.Anything).Return(nil, errors.New("boom"))
_, err := NewLoader("k", "https://api.github.com", "1", "o", "r", mockClient).Load()
assert.EqualError(t, err, "Failed creating request to github: boom")
}
func TestLoader_Load_FailedDo(t *testing.T) {
mockClient := &pluginhttp.MockClient{}
request := httptest.NewRequest("GET", "http://anywhere", nil)
mockClient.On("NewRequest", mock.Anything, mock.Anything, mock.Anything).Return(request, nil)
mockClient.On("Do", request).Return(nil, errors.New("boom"))
_, err := NewLoader("k", "https://api.github.com", "1", "o", "r", mockClient).Load()
assert.EqualError(t, err, "Failed calling github: boom")
}
func TestLoader_Load_BadStatus(t *testing.T) {
mockClient := &pluginhttp.MockClient{}
request := httptest.NewRequest("GET", "http://anywhere", nil)
mockClient.On("NewRequest", mock.Anything, mock.Anything, mock.Anything).Return(request, nil)
mockClient.On("Do", request).Return(&http.Response{StatusCode: 404, Body: io.NopCloser(strings.NewReader(""))}, nil)
_, err := NewLoader("k", "https://api.github.com", "1", "o", "r", mockClient).Load()
assert.EqualError(t, err, "Failed calling github: bad status code: 404")
}