| // Copyright 2021 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package monorail |
| |
| import ( |
| "net/url" |
| "strings" |
| "testing" |
| ) |
| |
| func TestBuildQueryLink(t *testing.T) { |
| testCases := []struct { |
| name string |
| baseURL string |
| issueQuery IssueQuery |
| optionalQueryParams OptionalQueryParams |
| }{ |
| { |
| "Empty query", |
| "https://monorail-prod.appspot.com/_ah/api/monorail/v1/projects/fuchsia/issues", |
| IssueQuery{ |
| Can: All, |
| Labels: []string{}, |
| SearchString: "", |
| }, |
| OptionalQueryParams{}, |
| }, |
| { |
| "New can", |
| "https://monorail-prod.appspot.com/_ah/api/monorail/v1/projects/fuchsia/issues", |
| IssueQuery{ |
| Can: New, |
| Labels: []string{"deadbeef"}, |
| SearchString: "test", |
| }, |
| OptionalQueryParams{ |
| "key": "value", |
| }, |
| }, |
| { |
| "Multi-parameter query", |
| "https://monorail-prod.appspot.com/_ah/api/monorail/v1/projects/fuchsia/issues", |
| IssueQuery{ |
| Can: New, |
| Labels: []string{"deadbeef", "test", "abc123"}, |
| SearchString: "deadbeef/test/abc123.cc", |
| }, |
| OptionalQueryParams{ |
| "1337": "7331", |
| "param1": "a", |
| "param2": "b", |
| "param3": "c", |
| "key": "value", |
| }, |
| }, |
| } |
| for _, tc := range testCases { |
| t.Run(tc.name, func(t *testing.T) { |
| got := buildQueryLink(tc.baseURL, tc.issueQuery, tc.optionalQueryParams) |
| if wantedPrefix := tc.baseURL; !strings.HasPrefix(got, wantedPrefix) { |
| t.Errorf("buildQueryLink(%s, %s, %s): got %s, but wanted to have prefix %s", tc.baseURL, tc.issueQuery, tc.optionalQueryParams, got, wantedPrefix) |
| } |
| if wantedSuffix := url.QueryEscape(tc.issueQuery.SearchString); !strings.HasSuffix(got, wantedSuffix) { |
| t.Errorf("buildQueryLink(%s, %s, %s): got %s, but wanted to have suffix %s", tc.baseURL, tc.issueQuery, tc.optionalQueryParams, got, wantedSuffix) |
| } |
| }) |
| } |
| } |