Skip to content

Commit 799ac1c

Browse files
committed
Reimplement google short url resolver tests
1 parent 8a3173c commit 799ac1c

File tree

1 file changed

+38
-47
lines changed

1 file changed

+38
-47
lines changed

Diff for: test/google-tests.js

+38-47
Original file line numberDiff line numberDiff line change
@@ -23,68 +23,59 @@
2323
// POSSIBILITY OF SUCH DAMAGE.
2424

2525
const chai = require('chai'),
26-
chaiAsPromised = require("chai-as-promised"),
26+
chaiAsPromised = require('chai-as-promised'),
2727
nock = require('nock'),
2828
google = require('../lib/google');
2929

3030
chai.use(chaiAsPromised);
3131
chai.should();
3232

33-
const googleApiUrl = 'https://www.googleapis.com';
34-
const shortUrlEndpoint = '/urlshortener/v1/url';
33+
const googlDomain = 'https://goo.gl';
34+
const shortUrl = '/short';
3535

3636
describe('Google short URL resolver tests', () => {
37+
after(() => {
38+
nock.cleanAll();
39+
});
40+
3741
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/' });
5250
});
51+
5352
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+
6257
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');
6560
});
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+
7467
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');
7770
});
71+
7872
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+
8677
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+
});
9081
});

0 commit comments

Comments
 (0)