| 1 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | |
| 3 | package search |
| 4 | |
| 5 | import ( |
| 6 | "net/http/httptest" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestPageFromRequestUsesGitHubStylePParam(t *testing.T) { |
| 11 | req := httptest.NewRequest("GET", "/search?q=repo&p=3", nil) |
| 12 | if got := pageFromRequest(req); got != 3 { |
| 13 | t.Fatalf("pageFromRequest = %d, want 3", got) |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | func TestPageFromRequestAcceptsLegacyPageParam(t *testing.T) { |
| 18 | req := httptest.NewRequest("GET", "/search?q=repo&page=2", nil) |
| 19 | if got := pageFromRequest(req); got != 2 { |
| 20 | t.Fatalf("pageFromRequest = %d, want 2", got) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func TestSearchHrefEscapesQuery(t *testing.T) { |
| 25 | got := searchHref("repo:alice/demo pub", "issues", 4) |
| 26 | want := "/search?p=4&q=repo%3Aalice%2Fdemo+pub&type=issues" |
| 27 | if got != want { |
| 28 | t.Fatalf("searchHref = %q, want %q", got, want) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestNormalizeSearchTabAcceptsGitHubTypesAndLegacyAliases(t *testing.T) { |
| 33 | cases := map[string]string{ |
| 34 | "": "repositories", |
| 35 | "repos": "repositories", |
| 36 | "repositories": "repositories", |
| 37 | "pulls": "pullrequests", |
| 38 | "pullrequests": "pullrequests", |
| 39 | "code": "code", |
| 40 | "issues": "issues", |
| 41 | "users": "users", |
| 42 | "unknown-value": "repositories", |
| 43 | } |
| 44 | for input, want := range cases { |
| 45 | if got := normalizeSearchTab(input); got != want { |
| 46 | t.Fatalf("normalizeSearchTab(%q) = %q, want %q", input, got, want) |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 |