Skip to content

Commit cb9ec86

Browse files
committed
misc(#122): add unit test for jira request URI combine logic
1 parent 57d7e93 commit cb9ec86

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

internal/jira/jira_request.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import (
1010
)
1111

1212
func (api *httpApi) jiraRequest(method string, restPath string, queryParams interface{}, reqBody io.Reader) ([]byte, error) {
13-
queryParamsValues, err := query.Values(queryParams)
13+
u, err := api.jiraRequestUrl(restPath, queryParams)
1414
if err != nil {
1515
return nil, err
1616
}
17-
u := api.restUrl.ResolveReference(&url.URL{Path: path.Join(api.restUrl.Path, restPath), RawQuery: queryParamsValues.Encode()})
18-
req, err := http.NewRequest(method, u.String(), reqBody)
17+
req, err := http.NewRequest(method, u, reqBody)
1918
req.Header.Add("Accept", "application/json")
2019
req.Header.Add("Content-Type", "application/json")
2120
if err != nil {
@@ -32,3 +31,12 @@ func (api *httpApi) jiraRequest(method string, restPath string, queryParams inte
3231
body, _ := io.ReadAll(response.Body)
3332
return body, nil
3433
}
34+
35+
func (api *httpApi) jiraRequestUrl(restPath string, queryParams interface{}) (string, error) {
36+
queryParamsValues, err := query.Values(queryParams)
37+
if err != nil {
38+
return "", err
39+
}
40+
u := api.restUrl.ResolveReference(&url.URL{Path: path.Join(api.restUrl.Path, restPath), RawQuery: queryParamsValues.Encode()})
41+
return u.String(), err
42+
}

internal/jira/jira_request_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,26 @@ func Test_httpApi_jiraRequest_should_return_error_when_invalid_params(t *testing
3838
// then
3939
assert.NotNil(t, err)
4040
}
41+
42+
func Test_jiraRequest_combinePaths(t *testing.T) {
43+
tests := []struct {
44+
name string
45+
apiUrl string
46+
restPath string
47+
wanted string
48+
}{
49+
{"should add label without error", "http://localhost", "/api1", "http://localhost/api1"},
50+
{"should add label without error", "http://localhost/", "/api1", "http://localhost/api1"},
51+
{"should add label without error", "http://localhost/jira-api-v2", "/api1", "http://localhost/jira-api-v2/api1"},
52+
}
53+
for _, tt := range tests {
54+
t.Run(tt.name, func(t *testing.T) {
55+
u, _ := url.Parse(tt.apiUrl)
56+
api := &httpApi{restUrl: u}
57+
58+
result, _ := api.jiraRequestUrl(tt.restPath, nil)
59+
60+
assert.Equal(t, tt.wanted, result)
61+
})
62+
}
63+
}

0 commit comments

Comments
 (0)