|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import remark from 'remark'; |
| 9 | +import dedent from 'dedent'; |
| 10 | +import {jest} from '@jest/globals'; |
| 11 | + |
| 12 | +jest.unstable_mockModule('../lib.js', () => ({ |
| 13 | + fetch: jest.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +const {fetch} = await import('../lib.js'); |
| 17 | +const plugin = (await import('../')).default; |
| 18 | + |
| 19 | +const processMarkdown = (md, opts) => { |
| 20 | + return remark().use(plugin, opts).process(md); |
| 21 | +}; |
| 22 | + |
| 23 | +describe('remark-lint-no-dead-urls', () => { |
| 24 | + beforeEach(() => fetch.mockReset()); |
| 25 | + |
| 26 | + test('works with no URLs', () => { |
| 27 | + const lint = processMarkdown(dedent` |
| 28 | + # Title |
| 29 | +
|
| 30 | + No URLs in here. |
| 31 | + `); |
| 32 | + |
| 33 | + return lint.then(vFile => { |
| 34 | + expect(fetch).toHaveBeenCalledTimes(0); |
| 35 | + expect(vFile.messages.length).toBe(0); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + test('works a good, bad a local link', () => { |
| 40 | + fetch.mockReturnValueOnce(200).mockReturnValueOnce(404); |
| 41 | + |
| 42 | + const lint = processMarkdown( |
| 43 | + dedent` |
| 44 | + # Title |
| 45 | +
|
| 46 | + Here is a [good link](https://www.github.com). |
| 47 | +
|
| 48 | + Here is a [bad link](https://github.com/unified/oops). |
| 49 | +
|
| 50 | + Here is a [local link](http://localhost:3000). |
| 51 | + ` |
| 52 | + ); |
| 53 | + |
| 54 | + return lint.then(vFile => { |
| 55 | + expect(fetch).toHaveBeenCalledTimes(2); |
| 56 | + expect(vFile.messages.length).toBe(1); |
| 57 | + expect(vFile.messages[0].reason).toBe( |
| 58 | + 'Link to https://github.com/unified/oops is broken' |
| 59 | + ); |
| 60 | + }); |
| 61 | + }, 15000); |
| 62 | + |
| 63 | + test('works with definitions and images', () => { |
| 64 | + fetch.mockReturnValueOnce(200).mockReturnValueOnce(404); |
| 65 | + |
| 66 | + const lint = processMarkdown( |
| 67 | + dedent` |
| 68 | + # Title |
| 69 | +
|
| 70 | + Here is a good pig: . |
| 71 | +
|
| 72 | + Download the pig picture [here](/pig-photos/384). |
| 73 | +
|
| 74 | + Here is a [bad link]. Here is that [bad link] again. |
| 75 | +
|
| 76 | + [bad link]: /oops/broken |
| 77 | + `, |
| 78 | + { |
| 79 | + baseUrl: 'http://my.domain.com', |
| 80 | + } |
| 81 | + ); |
| 82 | + |
| 83 | + return lint.then(vFile => { |
| 84 | + expect(fetch).toHaveBeenCalledTimes(2); |
| 85 | + expect(vFile.messages.length).toBe(1); |
| 86 | + expect(vFile.messages[0].reason).toBe('Link to /oops/broken is broken'); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + test('skips URLs with unsupported protocols', () => { |
| 91 | + const lint = processMarkdown(dedent` |
| 92 | + [Send me an email.](mailto:me@me.com) |
| 93 | + [Look at this file.](ftp://path/to/file.txt) |
| 94 | + [Special schema.](flopper://a/b/c) |
| 95 | + `); |
| 96 | + |
| 97 | + return lint.then(vFile => { |
| 98 | + expect(fetch).toHaveBeenCalledTimes(0); |
| 99 | + expect(vFile.messages.length).toBe(0); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + test('localhost', () => { |
| 104 | + const lint = processMarkdown( |
| 105 | + dedent` |
| 106 | + - [http://localhost](http://localhost) |
| 107 | + - [http://localhost/alex/test](http://localhost/alex/test) |
| 108 | + - [http://localhost:3000](http://localhost:3000) |
| 109 | + - [http://localhost:3000/alex/test](http://localhost:3000/alex/test) |
| 110 | + - [https://localhost](http://localhost) |
| 111 | + - [https://localhost/alex/test](http://localhost/alex/test) |
| 112 | + - [https://localhost:3000](http://localhost:3000) |
| 113 | + - [https://localhost:3000/alex/test](http://localhost:3000/alex/test) |
| 114 | + ` |
| 115 | + ); |
| 116 | + |
| 117 | + return lint.then(vFile => { |
| 118 | + expect(vFile.messages.length).toBe(0); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + test('local IP 127.0.0.1', () => { |
| 123 | + const lint = processMarkdown( |
| 124 | + dedent` |
| 125 | + - [http://127.0.0.1](http://127.0.0.1) |
| 126 | + - [http://127.0.0.1:3000](http://127.0.0.1:3000) |
| 127 | + - [http://127.0.0.1/alex/test](http://127.0.0.1) |
| 128 | + - [http://127.0.0.1:3000/alex/test](http://127.0.0.1:3000) |
| 129 | + - [https://127.0.0.1](http://127.0.0.1) |
| 130 | + - [https://127.0.0.1:3000](http://127.0.0.1:3000) |
| 131 | + - [https://127.0.0.1/alex/test](http://127.0.0.1) |
| 132 | + - [https://127.0.0.1:3000/alex/test](http://127.0.0.1:3000) |
| 133 | + ` |
| 134 | + ); |
| 135 | + |
| 136 | + return lint.then(vFile => { |
| 137 | + expect(vFile.messages.length).toBe(0); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + test.each([ |
| 142 | + '[Ignore this](http://www.url-to-ignore.com)', |
| 143 | + '[Ignore this](http://www.url-to-ignore.com/somePath)', |
| 144 | + '[Ignore this](http://www.url-to-ignore.com/somePath?withQuery=wow)', |
| 145 | + '[its complicated](http://url-to-ignore.com/somePath/maybe)', |
| 146 | + ])('skipUrlPatterns for content: %s', markdownContent => { |
| 147 | + const lint = processMarkdown(markdownContent, { |
| 148 | + skipUrlPatterns: [/^http:\/\/(.*)url-to-ignore\.com/], |
| 149 | + }); |
| 150 | + |
| 151 | + return lint.then(vFile => { |
| 152 | + expect(vFile.messages.length).toBe(0); |
| 153 | + }); |
| 154 | + }); |
| 155 | +}); |
0 commit comments