-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
135 lines (125 loc) · 3.76 KB
/
index.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
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
/*jshint node: true */
'use strict';
var nock = require('nock');
var multiline = require('multiline');
/**
* GitHub OAuth host and port.
* @type {String}
*/
var defaultHost = 'https://github.com:443';
/**
* Mock access token response. Note: do not indent.
* @type {String}
*/
var tokenResponse = multiline(function () {/*
access_token=9999999999999999999999999999999999999999&scope=read%3Arepo_hook%2Crepo%2Cuser%3Aemail&token_type=bearer
*/
});
/**
* Intercept `/login`.
*/
function nockLogin(host) {
nock(host)
.filteringPath(/\/login\?.+/, '/login')
.get('/login')
.reply(200, '<html>login form...</html>', {
'set-cookie': true
});
}
/**
* Intercept `/login/oauth/authorize`.
*/
function nockAuthorize(host) {
nock(host)
.filteringPath(/\/login\/oauth\/authorize\?.+/, '/login/oauth/authorize')
.get('/login/oauth/authorize')
.reply(200, '<html>login form...</html>');
}
/**
* Intercept `/login/oauth/access_token`.
*/
function nockAccessToken(host) {
nock(host)
.filteringRequestBody(function () {
return '*';
})
.filteringPath(/\/login\/oauth\/access_token.+/, '/login/oauth/access_token')
.post(
'/login/oauth/access_token',
'*')
.reply(200, tokenResponse, {
server: 'github.com',
date: 'Tue, 24 Jun 2014 23:32:25 GMT',
'content-type': 'application/x-www-form-urlencoded; charset=utf-8',
status: '200 OK',
'cache-control': 'private, max-age=0, must-revalidate',
'x-xss-protection': '1; mode=block',
'x-frame-options': 'deny',
'content-security-policy': multiline(function () {/*
default-src *;
script-src assets-cdn.github.com www.google-analytics.com collector-cdn.github.com;
object-src assets-cdn.github.com;
style-src \'self\' \'unsafe-inline\' \'unsafe-eval\' assets-cdn.github.com;
img-src \'self\' data:
assets-cdn.github.com
identicons.github.com
www.google-analytics.com
collector.githubapp.com
*.githubusercontent.com
*.gravatar.com
*.wp.com;
media-src \'none\';
frame-src \'self\' render.githubusercontent.com www.youtube.com;
font-src assets-cdn.github.com;
connect-src \'self\' ghconduit.com:25035 live.github.com uploads.github.com s3.amazonaws.com'
*/
}),
vary: 'X-PJAX',
'set-cookie': [multiline(function () {/*
logged_in=no;
domain=.github.jparrowsec.cn;
path=/;
expires=Sat, 24-Jun-2034 23:32:25 GMT;
secure;
HttpOnly',
'_gh_sess=9999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999%3D%3D--33b04d33bc6e556945428bcc116c6a43b2db2598;
path=/; secure; HttpOnly
*/
})],
etag: '"a4ab5439e04d3a07cfeb781e3a97f4ab"',
'content-length': '116',
'x-github-request-id': '62D29D8A:4018:165A0FE4:53AA0A89',
'strict-transport-security': 'max-age=31536000',
'x-content-type-options': 'nosniff',
'x-served-by': '9835a984a05caa405eb61faaa1546741'
});
}
/**
* Setup nock intercepts for github OAuth.
* @param {Object} [options] Optional options for the nock setup.
* @param {Function} [cb] Optional callback to execute after intercepts have been created.
*/
function nockGithubOAuth(options, cb) {
if (typeof options == 'function') {
cb = options;
options = { host: defaultHost };
}
else if (typeof options == 'object') {
if (!options.host) {
options.host = defaultHost;
}
}
else {
options = { host: defaultHost };
}
nockLogin(options.host);
nockAuthorize(options.host);
nockAccessToken(options.host);
if (cb) { cb(); }
}
module.exports = {
nock: nockGithubOAuth,
token: tokenResponse,
host: defaultHost
};