|
23 | 23 | // POSSIBILITY OF SUCH DAMAGE. |
24 | 24 |
|
25 | 25 | const chai = require('chai'), |
26 | | - chaiAsPromised = require("chai-as-promised"), |
| 26 | + chaiAsPromised = require('chai-as-promised'), |
27 | 27 | nock = require('nock'), |
28 | 28 | google = require('../lib/google'); |
29 | 29 |
|
30 | 30 | chai.use(chaiAsPromised); |
31 | 31 | chai.should(); |
32 | 32 |
|
33 | | -const googleApiUrl = 'https://www.googleapis.com'; |
34 | | -const shortUrlEndpoint = '/urlshortener/v1/url'; |
| 33 | +const googlDomain = 'https://goo.gl'; |
| 34 | +const shortUrl = '/short'; |
35 | 35 |
|
36 | 36 | describe('Google short URL resolver tests', () => { |
| 37 | + after(() => { |
| 38 | + nock.cleanAll(); |
| 39 | + }); |
| 40 | + |
37 | 41 | const resolver = new google.ShortLinkResolver(); |
38 | | - // I'll add them in due time, I promise :) |
39 | | - // This ones are for the older revision pre goo.gl shutdown |
40 | | - /*it('Resolves simple URLs', () => { |
41 | | - const resultObj = {longUrl: "http://long.url/", shortUrl: "badger"}; |
42 | | - nock(googleApiUrl) |
43 | | - .get(shortUrlEndpoint) |
44 | | - .query({ |
45 | | - key: 'GoogleApiKey', |
46 | | - shortUrl: 'https://goo.gl/short' |
47 | | - }) |
48 | | - .reply(200, JSON.stringify(resultObj)); |
49 | | - return resolver |
50 | | - .resolve('https://goo.gl/short') |
51 | | - .should.eventually.deep.equal(resultObj); |
| 42 | + |
| 43 | + it('Resolves simple URLs', async () => { |
| 44 | + nock(googlDomain) |
| 45 | + .head(shortUrl) |
| 46 | + .reply(302, {}, { location: 'http://long.url/' }); |
| 47 | + |
| 48 | + const resp = await resolver.resolve(googlDomain + shortUrl); |
| 49 | + resp.should.deep.equal({ longUrl: 'http://long.url/' }); |
52 | 50 | }); |
| 51 | + |
53 | 52 | it('Handles missing long urls', () => { |
54 | | - const resultObj = {missing: "no long url"}; |
55 | | - nock(googleApiUrl) |
56 | | - .get(shortUrlEndpoint) |
57 | | - .query({ |
58 | | - key: 'GoogleApiKey', |
59 | | - shortUrl: 'https://goo.gl/broken' |
60 | | - }) |
61 | | - .reply(200, JSON.stringify(resultObj)); |
| 53 | + nock(googlDomain) |
| 54 | + .head(shortUrl) |
| 55 | + .reply(404); |
| 56 | + |
62 | 57 | return resolver |
63 | | - .resolve('https://goo.gl/broken') |
64 | | - .should.be.rejectedWith("Missing longUrl"); |
| 58 | + .resolve(googlDomain + shortUrl) |
| 59 | + .should.be.rejectedWith('Got response 404'); |
65 | 60 | }); |
66 | | - it('Handles unsuccessful requests', () => { |
67 | | - nock(googleApiUrl) |
68 | | - .get(shortUrlEndpoint) |
69 | | - .query({ |
70 | | - key: 'GoogleApiKey', |
71 | | - shortUrl: 'https://goo.gl/broken' |
72 | | - }) |
73 | | - .reply(404, "Google says no"); |
| 61 | + |
| 62 | + it('Handles missing location header', () => { |
| 63 | + nock(googlDomain) |
| 64 | + .head(shortUrl) |
| 65 | + .reply(302); |
| 66 | + |
74 | 67 | return resolver |
75 | | - .resolve('https://goo.gl/broken') |
76 | | - .should.be.rejectedWith("Got response 404"); |
| 68 | + .resolve(googlDomain + shortUrl) |
| 69 | + .should.be.rejectedWith('Missing location url in undefined'); |
77 | 70 | }); |
| 71 | + |
78 | 72 | it('Handles failed requests', () => { |
79 | | - nock(googleApiUrl) |
80 | | - .get(shortUrlEndpoint) |
81 | | - .query({ |
82 | | - key: 'GoogleApiKey', |
83 | | - shortUrl: 'https://goo.gl/broken' |
84 | | - }) |
85 | | - .replyWithError("Something went wrong"); |
| 73 | + nock(googlDomain) |
| 74 | + .head(shortUrl) |
| 75 | + .replyWithError('Something went wrong'); |
| 76 | + |
86 | 77 | return resolver |
87 | | - .resolve('https://goo.gl/broken') |
88 | | - .should.be.rejectedWith("Something went wrong"); |
89 | | - });*/ |
| 78 | + .resolve(googlDomain + shortUrl) |
| 79 | + .should.be.rejectedWith('Something went wrong'); |
| 80 | + }); |
90 | 81 | }); |
0 commit comments