Skip to content

Commit 3696334

Browse files
committed
Added support for complex relative urls i.e. './api/products' or '../api/products'
1 parent 12fe0f7 commit 3696334

File tree

8 files changed

+68
-6
lines changed

8 files changed

+68
-6
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-http-batcher",
3-
"version": "1.9.0",
3+
"version": "1.10.0",
44
"description": "Enables transparent HTTP batch requests with Angular",
55
"main": "dist/angular-http-batch.js",
66
"keywords": [

dist/ChangeLog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
15/07/2015 V1.10.0
2+
Added support for complex relative urls i.e. './api/products' or '../api/products'
3+
14
15/07/2015 V1.9.0
25
A function can now be added to the config object to override the default mechanism used to determine if a call should be batched.
36
Add a canBatchRequest function to the config object which takes in a url and http method type and return true if the call can be batched.

dist/angular-http-batch.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* angular-http-batcher - v1.9.0 - 2015-07-15
2+
* angular-http-batcher - v1.10.0 - 2015-07-15
33
* https://github.com/jonsamwell/angular-http-batcher
44
* Copyright (c) 2015 Jon Samwell
55
*/
@@ -440,6 +440,13 @@ angular.module(window.ahb.name).factory('httpBatcher', [
440440
protocolEndIndex,
441441
urlParts;
442442

443+
if (url.indexOf('./') > -1 || url.indexOf('../') > -1) {
444+
// we have a complex relative url i.e. './api/products' or '../api/products
445+
var parser = document.createElement('a');
446+
parser.href = url;
447+
url = parser.href;
448+
}
449+
443450
if (url.indexOf('://') > -1) {
444451
protocolEndIndex = url.indexOf('://') + 3;
445452
urlParts = url.slice(protocolEndIndex).split(constants.forwardSlash);

dist/angular-http-batch.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-http-batcher",
3-
"version": "1.9.0",
3+
"version": "1.10.0",
44
"description": "Enables transparent HTTP batch requests with Angular",
55
"main": "angular-http-batcher.min.js",
66
"scripts": {

src/services/httpBatcher.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,13 @@ angular.module(window.ahb.name).factory('httpBatcher', [
266266
protocolEndIndex,
267267
urlParts;
268268

269+
if (url.indexOf('./') > -1 || url.indexOf('../') > -1) {
270+
// we have a complex relative url i.e. './api/products' or '../api/products
271+
var parser = document.createElement('a');
272+
parser.href = url;
273+
url = parser.href;
274+
}
275+
269276
if (url.indexOf('://') > -1) {
270277
protocolEndIndex = url.indexOf('://') + 3;
271278
urlParts = url.slice(protocolEndIndex).split(constants.forwardSlash);

tests/index.html

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<div ng-controller="mainCtrl">
1010
<button type="button" ng-click="callSingle();">Call Single API Method</button>
1111
<button type="button" ng-click="setCookie();">Set Server Cookie</button>
12+
<button type="button" ng-click="tryRelativeUrl();">Try Relative Url</button>
1213
</div>
1314

1415
<script src="../bower_components/angular/angular.js"></script>
@@ -34,9 +35,20 @@
3435
'http://localhost:8080',
3536
'http://localhost:8080/api/batch', {
3637
batchRequestCollectionDelay: 500,
37-
minimumBatchSize: 2,
38+
minimumBatchSize: 1,
3839
sendCookies: true
3940
});
41+
// relative endpoint
42+
httpBatchConfigProvider.setAllowedBatchEndpoint(
43+
'./',
44+
'http://localhost:60000/api/batch', {
45+
batchRequestCollectionDelay: 500,
46+
minimumBatchSize: 1,
47+
sendCookies: true,
48+
canBatchRequest: function() {
49+
return true;
50+
}
51+
});
4052
}
4153
]);
4254

@@ -86,6 +98,15 @@
8698
console.log('error 2 - ' + angular.toJson(err));
8799
});
88100
};
101+
102+
$scope.tryRelativeUrl = function() {
103+
debugger;
104+
$http.get('./api/products').then(function (data) {
105+
console.log('success 0 - ' + data.data);
106+
}, function (err) {
107+
console.log('error 0 - ' + angular.toJson(err));
108+
});
109+
}
89110
}]);
90111
</script>
91112

tests/services/httpBatcher.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,30 @@
125125
$httpBackend.flush();
126126
});
127127

128+
129+
it('should create the correct http post data for a complex relative url GET request', function () {
130+
var batchConfig = {
131+
batchEndpointUrl: 'api/batch',
132+
batchRequestCollectionDelay: 200,
133+
minimumBatchSize: 1
134+
},
135+
postData = '--some_boundary_mocked\r\nContent-Type: application/http; msgtype=request\r\n\r\nGET /resource HTTP/1.1\r\nHost: localhost:9876\r\n\r\n\r\n--some_boundary_mocked--',
136+
responseData = '';
137+
138+
$httpBackend.expectPOST(batchConfig.batchEndpointUrl, postData).respond(404, responseData);
139+
sandbox.stub(httpBatchConfig, 'calculateBoundary').returns('some_boundary_mocked');
140+
sandbox.stub(httpBatchConfig, 'getBatchConfig').returns(batchConfig);
141+
142+
httpBatcher.batchRequest({
143+
url: './resource',
144+
method: 'GET',
145+
callback: angular.noop
146+
});
147+
148+
$timeout.flush();
149+
$httpBackend.flush();
150+
});
151+
128152
it('should add additional headers to the batch request as defined in the config object', function () {
129153
var batchConfig = {
130154
batchEndpointUrl: 'http://www.someservice.com/batch',

0 commit comments

Comments
 (0)