This repository has been archived by the owner on Dec 8, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgithub-client-test.js
81 lines (67 loc) · 2.54 KB
/
github-client-test.js
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
'use strict';
var GitHubClient = require('../lib/github-client');
var ApiStubServer = require('./helpers/api-stub-server');
var helpers = require('./helpers');
var url = require('url');
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.should();
chai.use(chaiAsPromised);
describe('GitHubClient', function() {
var githubClient, githubApiServer, setResponse;
before(function() {
var config = helpers.baseConfig();
githubApiServer = new ApiStubServer();
config.githubApiBaseUrl = githubApiServer.address();
githubClient = new GitHubClient(config);
});
after(function() {
githubApiServer.close();
});
afterEach(function() {
githubApiServer.urlsToResponses = {};
});
setResponse = function(statusCode, payload) {
var metadata = helpers.metadata();
githubApiServer.urlsToResponses['/repos/18F/handbook/issues'] = {
expectedParams: {
title: metadata.title,
body: metadata.url
},
statusCode: statusCode,
payload: payload
};
};
describe('API base URL', function() {
it('should parse the local server URL', function() {
url.format(githubClient.baseurl).should.eql(
githubApiServer.address() + '/');
githubClient.requestFactory.globalAgent.protocol.should.eql('http:');
});
it('should parse API_BASE_URL if config base URL undefined', function() {
var githubClient = new GitHubClient(helpers.baseConfig());
url.format(githubClient.baseurl).should.eql(GitHubClient.API_BASE_URL);
githubClient.requestFactory.globalAgent.protocol.should.eql('https:');
});
});
it('should successfully file an issue', function() {
setResponse(201, { 'html_url': helpers.ISSUE_URL });
return githubClient.fileNewIssue(helpers.metadata(), 'handbook')
.should.eventually.equal(helpers.ISSUE_URL);
});
it('should fail to make a request if the server is down', function() {
var config = helpers.baseConfig(),
githubClient;
config.githubApiBaseUrl = 'http://localhost';
githubClient = new GitHubClient(config);
return githubClient.fileNewIssue(helpers.metadata(), 'handbook')
.should.be.rejectedWith('failed to make GitHub API request:');
});
it('should receive an error when filing an issue', function() {
var payload = { message: 'test failure' };
setResponse(500, payload);
return githubClient.fileNewIssue(helpers.metadata(), 'handbook')
.should.be.rejectedWith(Error, 'received 500 response from GitHub ' +
'API: ' + JSON.stringify(payload));
});
});