From cb9ec866fd9723a2f168f895246219cfcf5b35fd Mon Sep 17 00:00:00 2001 From: mk-5 Date: Tue, 12 Nov 2024 23:05:04 +0100 Subject: [PATCH] misc(#122): add unit test for jira request URI combine logic --- internal/jira/jira_request.go | 14 +++++++++++--- internal/jira/jira_request_test.go | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/internal/jira/jira_request.go b/internal/jira/jira_request.go index 91e5cb5..aeac127 100644 --- a/internal/jira/jira_request.go +++ b/internal/jira/jira_request.go @@ -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 { @@ -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 +} diff --git a/internal/jira/jira_request_test.go b/internal/jira/jira_request_test.go index 67bc990..782b7c9 100644 --- a/internal/jira/jira_request_test.go +++ b/internal/jira/jira_request_test.go @@ -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) + }) + } +}