8000 Merge pull request #28 from manosim/refactor-http-args · ryanrain2016/javascript-client@467d532 · GitHub
[go: up one dir, main page]

Skip to content

Commit 467d532

Browse files
authored
Merge pull request core-api#28 from manosim/refactor-http-args
Refactor HTTP Transport arguments
2 parents 854cd60 + 1a38955 commit 467d532

File tree

5 files changed

+70
-32
lines changed

5 files changed

+70
-32
lines changed

lib/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function lookupLink (node, keys) {
2424
class Client {
2525
constructor (decoders = null, transports = null, csrf = null) {
2626
this.decoders = decoders || [new codecs.CoreJSONCodec(), new codecs.JSONCodec(), new codecs.TextCodec()]
27-
this.transports = transports || [new transportsModule.HTTPTransport(csrf)]
27+
this.transports = transports || [new transportsModule.HTTPTransport({csrf: csrf})]
2828
}
2929

3030
action (document, keys, params = {}) {

lib/transports/http.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ const parseResponse = (response, decoders) => {
1414
}
1515

1616
class HTTPTransport {
17-
constructor (csrf, _fetch) {
17+
constructor (options = {}) {
1818
this.schemes = ['http', 'https']
19-
this.csrf = csrf
20-
this.fetch = _fetch || fetch
19+
this.csrf = options.csrf
20+
this.fetch = options.fetch || fetch
2121
}
2222

2323
action (link, decoders, params = {}) {

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@
4545
"url-template": "^2.0.8"
4646
},
4747
"devDependencies": {
48-
"babel-jest": "^17.0.0",
49-
"babel-preset-es2015": "^6.16.0",
48+
"babel-jest": "^18.0.0",
49+
"babel-preset-es2015": "^6.18.0",
5050
"babelify": "^7.3.0",
51-
"browserify": "^13.1.0",
52-
"jest": "^17.0.0",
53-
"standard": "^8.5.0",
54-
"watchify": "^3.7.0"
51+
"browserify": "^13.3.0",
52+
"jest": "^18.1.0",
53+
"standard": "^8.6.0",
54+
"watchify": "^3.8.0"
5555
}
5656
}

tests/client.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ const testUtils = require('./__helpers__/utils')
55

66
describe('Test the Client', function () {
77
it('should get the content of page (text/html)', function () {
8-
const fetch = testUtils.mockedFetch('Hello, world', 'text/html')
9-
const transport = new transportsModule.HTTPTransport(null, fetch)
8+
const mockedFetch = testUtils.mockedFetch('Hello, world', 'text/html')
9+
const transport = new transportsModule.HTTPTransport({
10+
fetch: mockedFetch
11+
})
1012
const client = new coreapi.Client(null, [transport])
1113
const url = 'http://example.com'
1214

@@ -21,8 +23,10 @@ describe('Test the Client', function () {
2123
})
2224

2325
it('should get the content of page (application/json)', function () {
24-
const fetch = testUtils.mockedFetch('{"text": "hello"}', 'application/json')
25-
const transport = new transportsModule.HTTPTransport(null, fetch)
26+
const mockedFetch = testUtils.mockedFetch('{"text": "hello"}', 'application/json')
27+
const transport = new transportsModule.HTTPTransport({
28+
fetch: mockedFetch
29+
})
2630
const client = new coreapi.Client(null, [transport])
2731
const url = 'http://example.com'
2832

@@ -37,8 +41,10 @@ describe('Test the Client', function () {
3741
})
3842

3943
it('action should get the content of page (application/json)', function () {
40-
const fetch = testUtils.mockedFetch('{"text": "hello"}', 'application/json')
41-
const transport = new transportsModule.HTTPTransport(null, fetch)
44+
const mockedFetch = testUtils.mockedFetch('{"text": "hello"}', 'application/json')
45+
const transport = new transportsModule.HTTPTransport({
46+
fetch: mockedFetch
47+
})
4248
const client = new coreapi.Client(null, [transport])
4349
const document = new coreapi.Document('', '', '', {nested: {link: new coreapi.Link('http://example.com', 'get')}})
4450

@@ -53,8 +59,10 @@ describe('Test the Client', function () {
5359
})
5460

5561
it('action should raise an error for invalid link keys', function () {
56-
const fetch = testUtils.mockedFetch('{"text": "hello"}', 'application/json')
57-
const transport = new transportsModule.HTTPTransport(null, fetch)
62+
const mockedFetch = testUtils.mockedFetch('{"text": "hello"}', 'application/json')
63+
const transport = new transportsModule.HTTPTransport({
64+
fetch: mockedFetch
65+
})
5866
const client = new coreapi.Client(null, [transport])
5967
const document = new coreapi.Document('', '', '', {nested: {link: new coreapi.Link('http://example.com', 'get')}})
6068

tests/transports/http.js

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ describe('Test the HTTPTransport', function () {
1010
it('should check the action function of an HTTP transport (text/html)', function () {
1111
const url = 'http://www.example.com/'
1212
const link = new document.Link(url, 'get')
13-
const transport = new transports.HTTPTransport(null, testUtils.mockedFetch('Hello, world', 'text/html'))
13+
const transport = new transports.HTTPTransport({
14+
fetch: testUtils.mockedFetch('Hello, world', 'text/html')
15+
})
1416

1517
return transport.action(link, decoders)
1618
.then((res) => {
@@ -21,7 +23,9 @@ describe('Test the HTTPTransport', function () {
2123
it('should check the action function of an HTTP transport (json)', function () {
2224
const url = 'http://www.example.com/'
2325
const link = new document.Link(url, 'get')
24-
const transport = new transports.HTTPTransport(null, testUtils.mockedFetch('{"text": "Hello, world"}', 'application/json'))
26+
const transport = new transports.HTTPTransport({
27+
fetch: testUtils.mockedFetch('{"text": "Hello, world"}', 'application/json')
28+
})
2529

2630
return transport.action(link, decoders)
2731
.then(res => expect(res).toEqual({text: 'Hello, world'}))
@@ -40,7 +44,9 @@ describe('Test the HTTPTransport', function () {
4044
const url = 'http://www.example.com/'
4145
const fields = [new document.Field('firstField', true, 'form'), new document.Field('secondField', true, 'form')]
4246
const link = new document.Link(url, 'post', 'application/x-www-form-urlencoded', fields)
43-
const transport = new transports.HTTPTransport(null, testUtils.echo)
47+
const transport = new transports.HTTPTransport({
48+
fetch: testUtils.echo
49+
})
4450
const params = {
4551
firstField: 'hello',
4652
secondField: 'world'
@@ -60,7 +66,9 @@ describe('Test the HTTPTransport', function () {
6066
it('should check the action function of an HTTP transport (network fail)', function () {
6167
const url = 'http://www.example.com/'
6268
const link = new document.Link(url, 'get')
63-
const transport = new transports.HTTPTransport(null, testUtils.mockedFetch('ERROR', 'text/html', 500))
69+
const transport = new transports.HTTPTransport({
70+
fetch: testUtils.mockedFetch('ERROR', 'text/html', 500)
71+
})
6472

6573
return transport.action(link, decoders)
6674
.catch(function (result) {
@@ -73,7 +81,9 @@ describe('Test the HTTPTransport', function () {
7381
const url = 'http://www.example.com/'
7482
const fields = [new document.Field('page', false, 'query')]
7583
const link = new document.Link(url, 'get', 'application/json', fields)
76-
const transport = new transports.HTTPTransport(null, testUtils.echo)
84+
const transport = new transports.HTTPTransport({
85+
fetch: testUtils.echo
86+
})
7787
const params = {
7888
page: 23
7989
}
@@ -88,7 +98,9 @@ describe('Test the HTTPTransport', function () {
8898
const url = 'http://www.example.com/{user}/'
8999
const fields = [new document.Field('user', true, 'path')]
90100
const link = new document.Link(url, 'get', 'application/json', fields)
91-
const transport = new transports.HTTPTransport(null, testUtils.echo)
101+
const transport = new transports.HTTPTransport({
102+
fetch: testUtils.echo
103+
})
92104
const params = {
93105
user: 23
94106
}
@@ -102,7 +114,9 @@ describe('Test the HTTPTransport', function () {
102114
it('should check the action function of an HTTP transport (json) with post request', function () {
103115
const url = 'http://www.example.com/'
104116
const link = new document.Link(url, 'post')
105-
const transport = new transports.HTTPTransport(null, testUtils.echo)
117+
const transport = new transports.HTTPTransport({
118+
fetch: testUtils.echo
119+
})
106120

107121
return transport.action(link, decoders)
108122
.then((res) => {
@@ -114,7 +128,10 @@ describe('Test the HTTPTransport', function () {
114128
const url = 'http://www.example.com/'
115129
const link = new document.Link(url, 'post')
116130
const csrf = {'X-CSRFToken': 'abc'}
117-
const transport = new transports.HTTPTransport(csrf, testUtils.echo)
131+
const transport = new transports.HTTPTransport({
132+
csrf: csrf,
133+
fetch: testUtils.echo
134+
})
118135

119136
return transport.action(link, decoders)
120137
.then((res) => {
@@ -126,7 +143,10 @@ describe('Test the HTTPTransport', function () {
126143
const url = 'http://www.example.com/'
127144
const link = new document.Link(url, 'get')
128145
const csrf = {'X-CSRFToken': 'abc'}
129-
const transport = new transports.HTTPTransport(csrf, testUtils.echo)
146+
const transport = new transports.HTTPTransport({
147+
csrf: csrf,
148+
fetch: testUtils.echo
149+
})
130150

131151
return transport.action(link, decoders)
132152
.then((res) => {
@@ -138,7 +158,9 @@ describe('Test the HTTPTransport', function () {
138158
const url = 'http://www.example.com/'
139159
const fields = [new document.Field('hello', true, 'form')]
140160
const link = new document.Link(url, 'post', 'application/json', fields)
141-
const transport = new transports.HTTPTransport(null, testUtils.echo)
161+
const transport = new transports.HTTPTransport({
162+
fetch: testUtils.echo
163+
})
142164
const params = {
143165
hello: 'world'
144166
}
@@ -153,7 +175,9 @@ describe('Test the HTTPTransport', function () {
153175
const url = 'http://www.example.com/'
154176
const fields = [new document.Field('hello', true, 'body')]
155177
const link = new document.Link(url, 'post', 'application/json', fields)
156-
const transport = new transports.HTTPTransport(null, testUtils.echo)
178+
const transport = new transports.HTTPTransport({
179+
fetch: testUtils.echo
180+
})
157181
const params = {
158182
hello: 'world'
159183
}
@@ -168,7 +192,9 @@ describe('Test the HTTPTransport', function () {
168192
const url = 'http://www.example.com/'
169193
const fields = [new document.Field('page', false, 'query')]
170194
const link = new document.Link(url, 'get', 'application/json', fields)
171-
const transport = new transports.HTTPTransport(null, testUtils.echo)
195+
const transport = new transports.HTTPTransport({
196+
fetch: testUtils.echo
197+
})
172198
const params = {}
173199

174200
return transport.action(link, decoders, params)
@@ -181,7 +207,9 @@ describe('Test the HTTPTransport', function () {
181207
const url = 'http://www.example.com/{user}/'
182208
const fields = [new document.Field('user', true, 'path')]
183209
const link = new document.Link(url, 'get', 'application/json', fields)
184-
const transport = new transports.HTTPTransport(null, testUtils.echo)
210+
const transport = new transports.HTTPTransport({
211+
fetch: testUtils.echo
212+
})
185213
const params = {}
186214

187215
const callTransport = () => transport.action(link, decoders, params)
@@ -191,7 +219,9 @@ describe('Test the HTTPTransport', function () {
191219
it('should check the action function of an HTTP transport (json) with unknown paramater', function () {
192220
const url = 'http://www.example.com/'
193221
const link = new document.Link(url, 'get')
194-
const transport = new transports.HTTPTransport(null, testUtils.echo)
222+
const transport = new transports.HTTPTransport({
223+
fetch: testUtils.echo
224+
})
195225
const params = {
196226
hello: 'world'
197227
}

0 commit comments

Comments
 (0)
0