forked from OP-Engineering/link-preview-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.spec.ts
296 lines (260 loc) · 11.2 KB
/
index.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import { getLinkPreview, getPreviewFromContent } from "../index";
import prefetchedResponse from "./sampleResponse.json";
describe(`#getLinkPreview()`, () => {
it(`should extract link info from just URL`, async () => {
const linkInfo: any = await getLinkPreview(
`https://www.youtube.com/watch?v=wuClZjOdT30`,
{ headers: { "Accept-Language": `en-US` } }
);
expect(linkInfo.url).toEqual(`https://www.youtube.com/watch?v=wuClZjOdT30`);
expect(linkInfo.siteName).toEqual(`YouTube`);
expect(linkInfo.title).toEqual(`Geography Now! Germany`);
expect(linkInfo.description).toBeTruthy();
expect(linkInfo.mediaType).toEqual(`video.other`);
expect(linkInfo.images.length).toEqual(1);
expect(linkInfo.images[0]).toEqual(
`https://i.ytimg.com/vi/wuClZjOdT30/maxresdefault.jpg`
);
expect(linkInfo.videos.length).toEqual(0);
expect(linkInfo.favicons[0]).not.toBe(``);
expect(linkInfo.contentType.toLowerCase()).toEqual(`text/html`);
expect(linkInfo.charset?.toLowerCase()).toEqual(`utf-8`);
});
it("returns charset of website", async () => {
const linkInfo: any = await getLinkPreview(`https://www.pravda.com.ua`);
expect(linkInfo.url).toEqual(`https://www.pravda.com.ua/`);
expect(linkInfo.contentType.toLowerCase()).toEqual(`text/html`);
expect(linkInfo.charset?.toLowerCase()).toEqual(`utf-8`);
});
it(`should extract link info from a URL with a newline`, async () => {
const linkInfo: any = await getLinkPreview(
`
https://www.youtube.com/watch?v=wuClZjOdT30
`,
{ headers: { "Accept-Language": `en-US` } }
);
expect(linkInfo.url).toEqual(`https://www.youtube.com/watch?v=wuClZjOdT30`);
expect(linkInfo.title).toEqual(`Geography Now! Germany`);
expect(linkInfo.siteName).toBeTruthy();
expect(linkInfo.description).toBeTruthy();
expect(linkInfo.mediaType).toEqual(`video.other`);
expect(linkInfo.images.length).toEqual(1);
expect(linkInfo.images[0]).toEqual(
`https://i.ytimg.com/vi/wuClZjOdT30/maxresdefault.jpg`
);
expect(linkInfo.videos.length).toEqual(0);
expect(linkInfo.favicons[0]).not.toBe(``);
expect(linkInfo.contentType.toLowerCase()).toEqual(`text/html`);
});
it(`should extract link info from just text with a URL`, async () => {
const linkInfo: any = await getLinkPreview(
`This is some text blah blah https://www.youtube.com/watch?v=wuClZjOdT30 and more text`,
{ headers: { "Accept-Language": `en-US` } }
);
expect(linkInfo.url).toEqual(`https://www.youtube.com/watch?v=wuClZjOdT30`);
expect(linkInfo.title).toEqual(`Geography Now! Germany`);
expect(linkInfo.siteName).toEqual(`YouTube`);
expect(linkInfo.description).toBeTruthy();
expect(linkInfo.mediaType).toEqual(`video.other`);
expect(linkInfo.images.length).toEqual(1);
expect(linkInfo.images[0]).toEqual(
`https://i.ytimg.com/vi/wuClZjOdT30/maxresdefault.jpg`
);
expect(linkInfo.videos.length).toEqual(0);
expect(linkInfo.favicons[0]).toBeTruthy();
expect(linkInfo.contentType.toLowerCase()).toEqual(`text/html`);
});
xit(`should make request with different languages`, async () => {
let linkInfo: any = await getLinkPreview(`https://www.wikipedia.org/`, {
headers: { "Accept-Language": `es` },
followRedirects: `follow`,
});
expect(linkInfo.title).toContain(`Wikipedia, la enciclopedia libre`);
linkInfo = await getLinkPreview(`https://www.wikipedia.org/`);
expect(linkInfo.title).toContain(`Wikipedia`);
});
it(`should handle audio urls`, async () => {
const linkInfo = await getLinkPreview(
`https://ondemand.npr.org/anon.npr-mp3/npr/atc/2007/12/20071231_atc_13.mp3`
);
expect(linkInfo.url).toEqual(
`https://ondemand.npr.org/anon.npr-mp3/npr/atc/2007/12/20071231_atc_13.mp3`
);
expect(linkInfo.mediaType).toEqual(`audio`);
expect(linkInfo.contentType?.toLowerCase()).toEqual(`audio/mpeg`);
expect(linkInfo.favicons[0]).toBeTruthy();
});
it(`should handle video urls`, async () => {
const linkInfo = await getLinkPreview(
`https://www.w3schools.com/html/mov_bbb.mp4`
);
expect(linkInfo.url).toEqual(`https://www.w3schools.com/html/mov_bbb.mp4`);
expect(linkInfo.mediaType).toEqual(`video`);
expect(linkInfo.contentType?.toLowerCase()).toEqual(`video/mp4`);
expect(linkInfo.favicons[0]).toBeTruthy();
});
it(`should handle image urls`, async () => {
const linkInfo = await getLinkPreview(
`https://media.npr.org/assets/img/2018/04/27/gettyimages-656523922nunes-4bb9a194ab2986834622983bb2f8fe57728a9e5f-s1100-c15.jpg`
);
expect(linkInfo.url).toEqual(
`https://media.npr.org/assets/img/2018/04/27/gettyimages-656523922nunes-4bb9a194ab2986834622983bb2f8fe57728a9e5f-s1100-c15.jpg`
);
expect(linkInfo.mediaType).toEqual(`image`);
expect(linkInfo.contentType?.toLowerCase()).toEqual(`image/jpeg`);
expect(linkInfo.favicons[0]).toBeTruthy();
});
it(`should handle unknown content type urls`, async () => {
const linkInfo = await getLinkPreview(`https://mjml.io/try-it-live`);
expect(linkInfo.url).toEqual(`https://mjml.io/try-it-live`);
expect(linkInfo.mediaType).toEqual(`website`);
});
// This site changed? it is not returning application any more but rather website
it.skip(`should handle application urls`, async () => {
const linkInfo = await getLinkPreview(
`https://assets.curtmfg.com/masterlibrary/56282/installsheet/CME_56282_INS.pdf`
);
expect(linkInfo.url).toEqual(
`https://assets.curtmfg.com/masterlibrary/56282/installsheet/CME_56282_INS.pdf`
);
expect(linkInfo.mediaType).toEqual(`application`);
expect(linkInfo.contentType?.toLowerCase()).toEqual(`application/pdf`);
expect(linkInfo.favicons[0]).toBeTruthy();
});
it(`no link in text should fail gracefully`, async () => {
await expect(
getLinkPreview(`no link`)
).rejects.toThrowErrorMatchingSnapshot();
});
it(`should handle malformed urls gracefully`, async () => {
await expect(
getLinkPreview(
`this is a malformed link: ahttps://www.youtube.com/watch?v=wuClZjOdT30`
)
).rejects.toThrowErrorMatchingSnapshot();
});
it(`should handle empty strings gracefully`, async () => {
await expect(getLinkPreview(``)).rejects.toThrowErrorMatchingSnapshot();
});
it.skip(`should handle a proxy url option`, async () => {
// origin header is required by cors-anywhere
const linkInfo: any = await getLinkPreview(
`https://www.youtube.com/watch?v=wuClZjOdT30`,
{
proxyUrl: `https://cors-anywhere.herokuapp.com/`,
headers: {
Origin: `http://localhost:8000`,
"Accept-Language": `en-US`,
},
}
);
expect(linkInfo.url).toEqual(`https://www.youtube.com/watch?v=wuClZjOdT30`);
expect(linkInfo.siteName).toEqual(`YouTube`);
expect(linkInfo.title).toEqual(`Geography Now! Germany`);
expect(linkInfo.description).toBeTruthy();
expect(linkInfo.mediaType).toEqual(`video.other`);
expect(linkInfo.images.length).toEqual(1);
expect(linkInfo.images[0]).toEqual(
`https://i.ytimg.com/vi/wuClZjOdT30/maxresdefault.jpg`
);
expect(linkInfo.videos.length).toEqual(0);
expect(linkInfo.favicons[0]).not.toBe(``);
expect(linkInfo.contentType.toLowerCase()).toEqual(`text/html`);
});
it("should timeout (default 3s) with infinite loading link", async () => {
try {
await getLinkPreview(
`https://www.gamestop.com/video-games/pc-gaming/components/cooling/products/hyper-212-rgb-black-edition-fan/185243.html?gclid=Cj0KCQjwraqHBhDsARIsAKuGZeECDlqkF2cxpcuS0xRxQmrv5BxFawWS_B51kiqehPf64_KlO0oyunsaAhn5EALw_wcB&gclsrc=aw.ds`
);
} catch (e: any) {
expect(e.message).toEqual("Request timeout");
}
});
it("should timeout (custom 1s) with infinite loading link", async () => {
try {
await getLinkPreview(
`https://www.gamestop.com/video-games/pc-gaming/components/cooling/products/hyper-212-rgb-black-edition-fan/185243.html?gclid=Cj0KCQjwraqHBhDsARIsAKuGZeECDlqkF2cxpcuS0xRxQmrv5BxFawWS_B51kiqehPf64_KlO0oyunsaAhn5EALw_wcB&gclsrc=aw.ds`,
{
timeout: 1000,
}
);
} catch (e: any) {
expect(e.message).toEqual("Request timeout");
}
});
it(`should handle followRedirects option is error`, async () => {
try {
await getLinkPreview(`http://google.com/`, { followRedirects: `error` });
} catch (e: any) {
expect(e.message).toEqual(
`fetch failed`
);
}
});
it(`should handle followRedirects option is manual but handleRedirects was not provided`, async () => {
try {
await getLinkPreview(`http://google.com/`, { followRedirects: `manual` });
} catch (e: any) {
expect(e.message).toEqual(
`link-preview-js followRedirects is set to manual, but no handleRedirects function was provided`
);
}
});
it(`should handle followRedirects option is manual with handleRedirects function`, async () => {
const response = await getLinkPreview(`http://google.com/`, {
followRedirects: `manual`,
handleRedirects: (baseURL: string, forwardedURL: string) => {
if (forwardedURL !== `http://www.google.com/`) {
return false;
}
return true;
},
});
expect(response.contentType).toEqual(`text/html`);
expect(response.url).toEqual(`http://www.google.com/`);
expect(response.mediaType).toEqual(`website`);
});
it("should handle video tags without type or secure_url tags", async () => {
const res: any = await getLinkPreview(
`https://newpathtitle.com/falling-markets-how-to-stop-buyer-from-getting-out/`,
{ followRedirects: `follow` }
);
expect(res.siteName).toEqual(`New Path Title`);
expect(res.title).toEqual(
`Falling Markets: How To Stop A Buyer From Getting Out | New Path Title`
);
expect(res.description).toBeTruthy();
expect(res.mediaType).toEqual(`article`);
expect(res.images.length).toBeGreaterThan(0);
expect(res.videos.length).toBeGreaterThan(0);
expect(res.videos[0].url).toEqual(
`https://www.youtube.com/embed/nqNXjxpAPkU`
);
expect(res.favicons.length).toBeGreaterThan(0);
expect(res.contentType.toLowerCase()).toEqual(`text/html`);
});
});
describe(`#getPreviewFromContent`, () => {
it(`Basic parsing`, async () => {
const linkInfo: any = await getPreviewFromContent(prefetchedResponse);
expect(linkInfo.url).toEqual(`https://www.youtube.com/watch?v=wuClZjOdT30`);
expect(linkInfo.siteName).toEqual(`YouTube`);
expect(linkInfo.title).toEqual(`Geography Now! Germany`);
expect(linkInfo.description).toBeTruthy();
expect(linkInfo.mediaType).toEqual(`video.other`);
expect(linkInfo.images.length).toEqual(1);
expect(linkInfo.images[0]).toEqual(
`https://i.ytimg.com/vi/wuClZjOdT30/maxresdefault.jpg`
);
expect(linkInfo.videos.length).toEqual(0);
expect(linkInfo.favicons[0]).not.toBe(``);
expect(linkInfo.contentType.toLowerCase()).toEqual(`text/html`);
});
});
xdescribe(`simple test`, () => {
it("fetch my repo", async () => {
const linkInfo: any = await getLinkPreview("https://www.pravda.com.ua");
console.warn({ linkInfo });
expect(1).toEqual(2);
});
});