Skip to content

Commit 7b3ffa0

Browse files
author
Carlos Atencio
committed
Test file refactoring
1 parent 4be943d commit 7b3ffa0

17 files changed

+924
-812
lines changed

Gruntfile.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ module.exports = function(grunt) {
5454
vendor: [
5555
'http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.js'
5656
],
57+
helpers: [
58+
'tests/setup.js'
59+
],
5760
template: 'tests/template.tmpl'
5861
}
5962
}

lib/httpMock.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ function getExpectationsString(expectations){
353353
return printExpectations.toString();
354354
}
355355

356-
module.exports = function(expectations){
356+
module.exports = function(expectations, plugins){
357357
var templateString = mockTemplate.toString();
358358
var template = templateString.substring(templateString.indexOf('{') + 1, templateString.lastIndexOf('}'));
359359
var newFunc = template.replace(/'<place_content_here>'/, '[' + getExpectationsString(expectations) + ']');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "protractor-http-mock",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "Mock HTTP calls in your protractor specs.",
55
"main": "index.js",
66
"scripts": {

tests/convenienceMethods.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
describe('convenience methods', function(){
2+
var module = window.__module, http;
3+
4+
beforeAll(function(){
5+
http = window.__getHttp();
6+
});
7+
8+
it('get', function(done){
9+
http.get('test-api.com/user').then(function(response){
10+
expect(response.data).toBe('pass');
11+
done();
12+
});
13+
});
14+
15+
it('post', function(done){
16+
http.post('/user', {
17+
name: 'Carlos'
18+
}).then(function(response){
19+
expect(response.data).toBe('Carlos has been saved');
20+
done();
21+
});
22+
});
23+
24+
it('head', function(done){
25+
http.head('/head').then(function(response){
26+
expect(response.data).toBe('head response');
27+
done();
28+
});
29+
});
30+
31+
it('put', function(done){
32+
http.put('/put').then(function(response){
33+
expect(response.data).toBe('put response');
34+
done();
35+
});
36+
});
37+
38+
it('delete', function(done){
39+
http.delete('/delete').then(function(response){
40+
expect(response.data).toBe('delete response');
41+
done();
42+
});
43+
});
44+
45+
it('jsonp', function(done){
46+
http.jsonp('/jsonp').then(function(response){
47+
expect(response.data).toBe('jsonp response');
48+
done();
49+
});
50+
});
51+
});

tests/directCalls.test.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
describe('direct calls', function(){
2+
var module = window.__module, http;
3+
4+
beforeAll(function(){
5+
http = window.__getHttp();
6+
});
7+
8+
describe('get', function(){
9+
it('captures a simple get call', function(done){
10+
http({
11+
method: 'GET',
12+
url: 'test-api.com/user'
13+
}).then(function(response){
14+
expect(response.data).toBe('pass');
15+
done();
16+
});
17+
});
18+
19+
it('captures get with provided params', function(done){
20+
http({
21+
method: 'GET',
22+
url: '/user',
23+
params: {
24+
id: 1
25+
}
26+
}).then(function(response){
27+
expect(response.data.name).toBe('Carlos Github');
28+
done();
29+
});
30+
});
31+
32+
it('treats params get with provided params', function(done){
33+
http({
34+
method: 'GET',
35+
url: '/user',
36+
params: {
37+
id: 2
38+
}
39+
}).then(function(response){
40+
expect(response.data).toBe('pass');
41+
done();
42+
});
43+
});
44+
45+
it('treats requests as GET by default', function(done){
46+
http({
47+
url: '/user',
48+
params: {
49+
id: 2
50+
}
51+
}).then(function(response){
52+
expect(response.data).toBe('pass');
53+
done();
54+
});
55+
});
56+
});
57+
58+
describe('post', function(){
59+
it('captures post calls', function(done){
60+
http({
61+
method: 'POST',
62+
url: '/user',
63+
data: {
64+
name: 'Carlos'
65+
}
66+
}).then(function(response){
67+
expect(response.data).toBe('Carlos has been saved');
68+
done();
69+
});
70+
});
71+
72+
it('captures expected post errors', function(done){
73+
http({
74+
method: 'POST',
75+
url: '/user',
76+
data: {
77+
name: 'Other name'
78+
}
79+
}).catch(function(response){
80+
expect(response.status).toBe(500);
81+
expect(response.data.error).toBe('Cant save other users');
82+
done();
83+
});
84+
});
85+
86+
it('treats data as optional field', function(done){
87+
http({
88+
method: 'POST',
89+
url: '/user',
90+
data: {
91+
some: 'thing'
92+
}
93+
}).then(function(response){
94+
expect(response.data).toBe('Generic match');
95+
done();
96+
});
97+
});
98+
});
99+
});

tests/externalUrl.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
describe('external url', function(){
2+
var module = window.__module, http;
3+
4+
beforeAll(function(){
5+
http = window.__getHttp();
6+
});
7+
8+
it('should match against external URLs', function(done){
9+
http({
10+
method: 'GET',
11+
url: 'https://test-api.com/user'
12+
}).then(function(response){
13+
expect(response.data).toBe('pass');
14+
done();
15+
});
16+
});
17+
});

tests/headers.test.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
describe('headers', function(){
2+
var module = window.__module, http;
3+
4+
beforeAll(function(){
5+
http = window.__getHttp();
6+
});
7+
8+
it('matches a request by headers', function(done){
9+
http({
10+
method: 'get',
11+
url: 'my-api.com/user',
12+
headers: {
13+
'x-auth': 'pass',
14+
'gzip-pro': 'yes'
15+
}
16+
}).then(function(response){
17+
expect(response.data).toBe('authentication passed');
18+
done();
19+
});
20+
});
21+
22+
it('can respond with headers', function(done){
23+
http({
24+
url: 'my-api.com/with-headers',
25+
method: 'get'
26+
}).then(function(response){
27+
expect(response.headers('X-AUTH')).toBe('authenticated');
28+
expect(response.headers('ANGULAR_API')).toBe('ang=api');
29+
done();
30+
});
31+
});
32+
33+
it('can respond with headers in convenience methods', function(done){
34+
http({
35+
url: 'my-api.com/with-headers',
36+
method: 'get'
37+
}).success(function(data, status, headers){
38+
expect(headers('X-AUTH')).toBe('authenticated');
39+
expect(headers('ANGULAR_API')).toBe('ang=api');
40+
done();
41+
});
42+
});
43+
44+
it('ignores header properties when their function return value is null', function(done){
45+
http({
46+
method: 'get',
47+
url: 'my-api.com/user',
48+
headers: {
49+
'x-auth': 'pass',
50+
'gzip-pro': 'yes',
51+
'ignore-me': function(){
52+
return null;
53+
}
54+
}
55+
}).then(function(response){
56+
expect(response.data).toBe('authentication passed');
57+
done();
58+
});
59+
});
60+
61+
it('always returns a header', function(done){
62+
http({
63+
method: 'GET',
64+
url: 'test-api.com/user'
65+
}).then(function(response){
66+
expect(response.headers).toBeDefined();
67+
expect(response.data).toBe('pass');
68+
done();
69+
});
70+
});
71+
72+
it('matches request by complex headers (include functions)', function(done){
73+
http({
74+
method: 'get',
75+
url: 'my-api.com/user',
76+
headers: {
77+
'x-auth': 'pass',
78+
'gzip-pro': function(config){
79+
if(config){
80+
return 'yes';
81+
}
82+
}
83+
}
84+
}).then(function(response){
85+
expect(response.data).toBe('authentication passed');
86+
done();
87+
});
88+
});
89+
90+
it('response headers function returns all headers if no provided header name', function(done){
91+
var expectedHeaders = {
92+
'X-AUTH': 'authenticated',
93+
'ANGULAR_API': 'ang=api'
94+
};
95+
96+
http({
97+
url: 'my-api.com/with-headers',
98+
method: 'get'
99+
}).then(function(response){
100+
expect(response.headers()).toEqual(expectedHeaders);
101+
expect(response.headers(null)).toEqual(expectedHeaders);
102+
expect(response.headers(undefined)).toEqual(expectedHeaders);
103+
done();
104+
});
105+
});
106+
});

0 commit comments

Comments
 (0)