|
| 1 | +package checks |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/url" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/xray-web/web-check-api/testutils" |
| 11 | +) |
| 12 | + |
| 13 | +func TestGetLinkedPages(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + testTargetURL := &url.URL{ |
| 16 | + Scheme: "http", |
| 17 | + Host: "internal.com", |
| 18 | + } |
| 19 | + testHTML := []byte(` |
| 20 | + <a href="http://internal.com/#heading"></a> |
| 21 | + <a href="//internal.com/1"></a> |
| 22 | + <a href="2"></a> |
| 23 | + <a href="/2"></a> |
| 24 | + <a href="http://external.com/1"></a> |
| 25 | + <a href="https://external.com/2"></a> |
| 26 | + <a href="http://external.com/2"></a> |
| 27 | + <a href="://external.com"></a> |
| 28 | + `) |
| 29 | + client := testutils.MockClient(testutils.Response(http.StatusOK, testHTML)) |
| 30 | + actualLinkedPagesData, err := NewLinkedPages(client).GetLinkedPages(context.TODO(), testTargetURL) |
| 31 | + assert.NoError(t, err) |
| 32 | + assert.Equal(t, LinkedPagesData{ |
| 33 | + Internal: []string{ |
| 34 | + "http://internal.com/2", |
| 35 | + "http://internal.com/#heading", |
| 36 | + "http://internal.com/1", |
| 37 | + }, |
| 38 | + External: []string{ |
| 39 | + "http://external.com/1", |
| 40 | + "http://external.com/2", |
| 41 | + "https://external.com/2", |
| 42 | + }, |
| 43 | + }, actualLinkedPagesData) |
| 44 | +} |
| 45 | + |
| 46 | +func TestResolveURL(t *testing.T) { |
| 47 | + t.Parallel() |
| 48 | + baseURL := url.URL{ |
| 49 | + Scheme: "http", |
| 50 | + Host: "example.com", |
| 51 | + } |
| 52 | + |
| 53 | + tests := []struct { |
| 54 | + name string |
| 55 | + href string |
| 56 | + expectedResolvedURL (*url.URL) |
| 57 | + expectedErrorExists bool |
| 58 | + }{ |
| 59 | + { |
| 60 | + name: "empty href", |
| 61 | + href: "", |
| 62 | + expectedResolvedURL: &url.URL{Scheme: "http", Host: "example.com"}, |
| 63 | + expectedErrorExists: false, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "missing scheme", |
| 67 | + href: "://example.com", |
| 68 | + expectedResolvedURL: nil, |
| 69 | + expectedErrorExists: true, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "relative scheme", |
| 73 | + href: "//example.com", |
| 74 | + expectedResolvedURL: &url.URL{Scheme: "http", Host: "example.com"}, |
| 75 | + expectedErrorExists: false, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "valid absolute url without path", |
| 79 | + href: "http://example.com", |
| 80 | + expectedResolvedURL: &url.URL{Scheme: "http", Host: "example.com"}, |
| 81 | + expectedErrorExists: false, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "valid absolute url with path", |
| 85 | + href: "http://example.com/123", |
| 86 | + expectedResolvedURL: &url.URL{Scheme: "http", Host: "example.com", Path: "/123"}, |
| 87 | + expectedErrorExists: false, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "valid relative url with leading slash", |
| 91 | + href: "/123", |
| 92 | + expectedResolvedURL: &url.URL{Scheme: "http", Host: "example.com", Path: "/123"}, |
| 93 | + expectedErrorExists: false, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "valid relative url without leading slash", |
| 97 | + href: "123", |
| 98 | + expectedResolvedURL: &url.URL{Scheme: "http", Host: "example.com", Path: "/123"}, |
| 99 | + expectedErrorExists: false, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "valid relative url edge case", |
| 103 | + href: "http//example.com", |
| 104 | + expectedResolvedURL: &url.URL{Scheme: "http", Host: "example.com", Path: "/http//example.com"}, |
| 105 | + expectedErrorExists: false, |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "valid relative url fragment", |
| 109 | + href: "#heading", |
| 110 | + expectedResolvedURL: &url.URL{Scheme: "http", Host: "example.com", Fragment: "heading"}, |
| 111 | + expectedErrorExists: false, |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + for _, tc := range tests { |
| 116 | + tc := tc |
| 117 | + t.Run(tc.name, func(t *testing.T) { |
| 118 | + t.Parallel() |
| 119 | + actualResolvedURL, err := resolveURL(&baseURL, tc.href) |
| 120 | + assert.Equal(t, tc.expectedResolvedURL, actualResolvedURL) |
| 121 | + if tc.expectedErrorExists { |
| 122 | + assert.Error(t, err) |
| 123 | + } else { |
| 124 | + assert.NoError(t, err) |
| 125 | + } |
| 126 | + }) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func TestSortURLsByFrequency(t *testing.T) { |
| 131 | + t.Parallel() |
| 132 | + testLinksMap := map[string]int{ |
| 133 | + "https://example.com": 1, |
| 134 | + "https://example2.com": 2, |
| 135 | + "https://example3.com": 3, |
| 136 | + } |
| 137 | + |
| 138 | + expectedSortedLinks := []string{ |
| 139 | + "https://example3.com", |
| 140 | + "https://example2.com", |
| 141 | + "https://example.com", |
| 142 | + } |
| 143 | + |
| 144 | + actualSortedLinks := sortURLsByFrequency(testLinksMap) |
| 145 | + assert.Equal(t, expectedSortedLinks, actualSortedLinks) |
| 146 | +} |
0 commit comments