Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/deputil/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

// URLChecker returns url if its status code is 200, otherwise returns empty string
func URLChecker(httpClient slackhttp.HTTPClient, url string) string {
resp, err := httpClient.Get(url)
resp, err := httpClient.Head(url)
if err != nil {
return ""
}
Expand Down
6 changes: 3 additions & 3 deletions internal/deputil/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ func Test_URLChecker(t *testing.T) {
expectedURL: "https://github.com/slack-samples/deno-starter-template",
setupHTTPClientMock: func(httpClientMock *slackhttp.HTTPClientMock) {
resOK := slackhttp.MockHTTPResponse(http.StatusOK, "OK")
httpClientMock.On("Get", mock.Anything).Return(resOK, nil)
httpClientMock.On("Head", mock.Anything).Return(resOK, nil)
},
},
"Returns an empty string when the HTTP status code is not 200": {
url: "https://github.com/slack-samples/template-not-found",
expectedURL: "",
setupHTTPClientMock: func(httpClientMock *slackhttp.HTTPClientMock) {
resNotFound := slackhttp.MockHTTPResponse(http.StatusNotFound, "Not Found")
httpClientMock.On("Get", mock.Anything).Return(resNotFound, nil)
httpClientMock.On("Head", mock.Anything).Return(resNotFound, nil)
},
},
"Returns an empty string when the HTTPClient has an error": {
url: "invalid_url",
expectedURL: "",
setupHTTPClientMock: func(httpClientMock *slackhttp.HTTPClientMock) {
httpClientMock.On("Get", mock.Anything).Return(nil, fmt.Errorf("HTTPClient error"))
httpClientMock.On("Head", mock.Anything).Return(nil, fmt.Errorf("HTTPClient error"))
},
},
}
Expand Down
16 changes: 8 additions & 8 deletions internal/pkg/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func Test_generateGitZipFileURL(t *testing.T) {
expectedURL: "https://github.com/slack-samples/deno-starter-template/archive/refs/heads/main.zip",
setupHTTPClientMock: func(httpClientMock *slackhttp.HTTPClientMock) {
res := slackhttp.MockHTTPResponse(http.StatusOK, "OK")
httpClientMock.On("Get", mock.Anything).Return(res, nil)
httpClientMock.On("Head", mock.Anything).Return(res, nil)
},
},
"Returns the zip URL using the master branch when no branch is provided and main branch doesn't exist": {
Expand All @@ -98,8 +98,8 @@ func Test_generateGitZipFileURL(t *testing.T) {
expectedURL: "https://github.com/slack-samples/deno-starter-template/archive/refs/heads/master.zip",
setupHTTPClientMock: func(httpClientMock *slackhttp.HTTPClientMock) {
res := slackhttp.MockHTTPResponse(http.StatusOK, "OK")
httpClientMock.On("Get", "https://github.com/slack-samples/deno-starter-template/archive/refs/heads/main.zip").Return(nil, fmt.Errorf("HttpClient error"))
httpClientMock.On("Get", "https://github.com/slack-samples/deno-starter-template/archive/refs/heads/master.zip").Return(res, nil)
httpClientMock.On("Head", "https://github.com/slack-samples/deno-starter-template/archive/refs/heads/main.zip").Return(nil, fmt.Errorf("HttpClient error"))
httpClientMock.On("Head", "https://github.com/slack-samples/deno-starter-template/archive/refs/heads/master.zip").Return(res, nil)
},
},
"Returns the zip URL using the specified branch when a branch is provided": {
Expand All @@ -108,7 +108,7 @@ func Test_generateGitZipFileURL(t *testing.T) {
expectedURL: "https://github.com/slack-samples/deno-starter-template/archive/refs/heads/pre-release-0316.zip",
setupHTTPClientMock: func(httpClientMock *slackhttp.HTTPClientMock) {
res := slackhttp.MockHTTPResponse(http.StatusOK, "OK")
httpClientMock.On("Get", mock.Anything).Return(res, nil)
httpClientMock.On("Head", mock.Anything).Return(res, nil)
},
},
"Returns an empty string when the HTTP status code is not 200": {
Expand All @@ -117,15 +117,15 @@ func Test_generateGitZipFileURL(t *testing.T) {
expectedURL: "",
setupHTTPClientMock: func(httpClientMock *slackhttp.HTTPClientMock) {
res := slackhttp.MockHTTPResponse(http.StatusNotFound, "Not Found")
httpClientMock.On("Get", mock.Anything).Return(res, nil)
httpClientMock.On("Head", mock.Anything).Return(res, nil)
},
},
"Returns an empty string when the HTTPClient has an error": {
templateURL: "https://github.com/slack-samples/deno-starter-template",
gitBranch: "",
expectedURL: "",
setupHTTPClientMock: func(httpClientMock *slackhttp.HTTPClientMock) {
httpClientMock.On("Get", mock.Anything).Return(nil, fmt.Errorf("HTTPClient error"))
httpClientMock.On("Head", mock.Anything).Return(nil, fmt.Errorf("HTTPClient error"))
},
},
"Returns the zip URL with .git suffix removed": {
Expand All @@ -134,7 +134,7 @@ func Test_generateGitZipFileURL(t *testing.T) {
expectedURL: "https://github.com/slack-samples/deno-starter-template/archive/refs/heads/main.zip",
setupHTTPClientMock: func(httpClientMock *slackhttp.HTTPClientMock) {
res := slackhttp.MockHTTPResponse(http.StatusOK, "OK")
httpClientMock.On("Get", mock.Anything).Return(res, nil)
httpClientMock.On("Head", mock.Anything).Return(res, nil)
},
},
"Returns the zip URL with .git inside URL preserved": {
Expand All @@ -143,7 +143,7 @@ func Test_generateGitZipFileURL(t *testing.T) {
expectedURL: "https://github.com/slack-samples/deno.git-starter-template/archive/refs/heads/main.zip",
setupHTTPClientMock: func(httpClientMock *slackhttp.HTTPClientMock) {
res := slackhttp.MockHTTPResponse(http.StatusOK, "OK")
httpClientMock.On("Get", mock.Anything).Return(res, nil)
httpClientMock.On("Head", mock.Anything).Return(res, nil)
},
},
}
Expand Down
1 change: 1 addition & 0 deletions internal/slackhttp/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
Get(url string) (*http.Response, error)
Head(url string) (*http.Response, error)
}

// HTTPClientOptions allows for the customization of a http.Client.
Expand Down
13 changes: 13 additions & 0 deletions internal/slackhttp/http_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ func (m *HTTPClientMock) Get(url string) (*http.Response, error) {
return httpResp, args.Error(1)
}

// Head is a mock that tracks the calls to Head and returns the mocked http.Response and error.
func (m *HTTPClientMock) Head(url string) (*http.Response, error) {
args := m.Called(url)

// http.Response can be nil when an error is provided.
var httpResp *http.Response
if _httpResp, ok := args.Get(0).(*http.Response); ok {
httpResp = _httpResp
}

return httpResp, args.Error(1)
}

// MockHTTPResponse is a helper that returns a mocked http.Response with the provided httpStatus and body.
func MockHTTPResponse(httpStatus int, body string) *http.Response {
resWriter := httptest.NewRecorder()
Expand Down
Loading