Skip to content

Commit

Permalink
misc(#122): add unit test for jira request URI combine logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mk-5 committed Nov 13, 2024
1 parent 57d7e93 commit cb9ec86
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
14 changes: 11 additions & 3 deletions internal/jira/jira_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import (
)

func (api *httpApi) jiraRequest(method string, restPath string, queryParams interface{}, reqBody io.Reader) ([]byte, error) {
queryParamsValues, err := query.Values(queryParams)
u, err := api.jiraRequestUrl(restPath, queryParams)
if err != nil {
return nil, err
}
u := api.restUrl.ResolveReference(&url.URL{Path: path.Join(api.restUrl.Path, restPath), RawQuery: queryParamsValues.Encode()})
req, err := http.NewRequest(method, u.String(), reqBody)
req, err := http.NewRequest(method, u, reqBody)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
if err != nil {
Expand All @@ -32,3 +31,12 @@ func (api *httpApi) jiraRequest(method string, restPath string, queryParams inte
body, _ := io.ReadAll(response.Body)
return body, nil
}

func (api *httpApi) jiraRequestUrl(restPath string, queryParams interface{}) (string, error) {
queryParamsValues, err := query.Values(queryParams)
if err != nil {
return "", err
}
u := api.restUrl.ResolveReference(&url.URL{Path: path.Join(api.restUrl.Path, restPath), RawQuery: queryParamsValues.Encode()})
return u.String(), err
}
23 changes: 23 additions & 0 deletions internal/jira/jira_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,26 @@ func Test_httpApi_jiraRequest_should_return_error_when_invalid_params(t *testing
// then
assert.NotNil(t, err)
}

func Test_jiraRequest_combinePaths(t *testing.T) {
tests := []struct {
name string
apiUrl string
restPath string
wanted string
}{
{"should add label without error", "http://localhost", "/api1", "http://localhost/api1"},
{"should add label without error", "http://localhost/", "/api1", "http://localhost/api1"},
{"should add label without error", "http://localhost/jira-api-v2", "/api1", "http://localhost/jira-api-v2/api1"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u, _ := url.Parse(tt.apiUrl)
api := &httpApi{restUrl: u}

result, _ := api.jiraRequestUrl(tt.restPath, nil)

assert.Equal(t, tt.wanted, result)
})
}
}

0 comments on commit cb9ec86

Please sign in to comment.