feat($resource): Make stripping of trailing slashes configurable.

First, this now uses a flat object configuration, similar to
`$httpBackend`.  This should make configuring this provider much more
familiar.

This adds a fourth optional argument to the `$resource()` constructor,
supporting overriding global `$resourceProvider` configuration.

Now, both of these ways of configuring this is supported:

    app.config(function($resourceProvider) {
      $resourceProvider.defaults.stripTrailingSlashes = false;
    });

or per instance:

    var CreditCard = $resource('/some/:url/', ..., ..., {
        stripTrailingSlashes: false
    });
This commit is contained in:
Vincent Driessen
2013-12-29 01:50:09 +01:00
committed by Michał Gołębiowski
parent acfcbdf906
commit 3878be52f6
2 changed files with 331 additions and 246 deletions

View File

@@ -1,9 +1,14 @@
'use strict';
describe("resource", function() {
var $resource, CreditCard, callback, $httpBackend;
var $resource, CreditCard, callback, $httpBackend, resourceProvider;
beforeEach(module('ngResource'));
beforeEach(module(function ($resourceProvider) {
resourceProvider = $resourceProvider;
}));
beforeEach(inject(function($injector) {
$httpBackend = $injector.get('$httpBackend');
$resource = $injector.get('$resource');
@@ -228,7 +233,6 @@ describe("resource", function() {
R.get({a: 'herp$'});
});
it('should not encode @ in url params', function() {
//encodeURIComponent is too agressive and doesn't follow http://www.ietf.org/rfc/rfc3986.txt
//with regards to the character set (pchar) allowed in path segments
@@ -240,7 +244,6 @@ describe("resource", function() {
R.get({a: 'doh@fo o', ':bar': '$baz@1', '!do&h': 'g=a h'});
});
it('should encode array params', function() {
var R = $resource('/Path/:a');
$httpBackend.expect('GET', '/Path/doh&foo?bar=baz1&bar=baz2').respond('{}');
@@ -253,6 +256,52 @@ describe("resource", function() {
R.get({a: 'null'});
});
it('should implicitly strip trailing slashes from URLs by default', function() {
var R = $resource('http://localhost:8080/Path/:a/');
$httpBackend.expect('GET', 'http://localhost:8080/Path/foo').respond();
R.get({a: 'foo'});
});
it('should support explicitly stripping trailing slashes from URLs', function() {
var R = $resource('http://localhost:8080/Path/:a/', {}, {}, {stripTrailingSlashes: true});
$httpBackend.expect('GET', 'http://localhost:8080/Path/foo').respond();
R.get({a: 'foo'});
});
it('should support explicitly keeping trailing slashes in URLs', function() {
var R = $resource('http://localhost:8080/Path/:a/', {}, {}, {stripTrailingSlashes: false});
$httpBackend.expect('GET', 'http://localhost:8080/Path/foo/').respond();
R.get({a: 'foo'});
});
it('should support provider-level configuration to strip trailing slashes in URLs', function() {
// Set the new behavior for all new resources created by overriding the
// provider configuration
resourceProvider.defaults.stripTrailingSlashes = false;
var R = $resource('http://localhost:8080/Path/:a/');
$httpBackend.expect('GET', 'http://localhost:8080/Path/foo/').respond();
R.get({a: 'foo'});
});
it('should support overriding provider default trailing-slash stripping configuration', function() {
// Set the new behavior for all new resources created by overriding the
// provider configuration
resourceProvider.defaults.stripTrailingSlashes = false;
// Specific instances of $resource can still override the provider's default
var R = $resource('http://localhost:8080/Path/:a/', {}, {}, {stripTrailingSlashes: true});
$httpBackend.expect('GET', 'http://localhost:8080/Path/foo').respond();
R.get({a: 'foo'});
});
it('should allow relative paths in resource url', function () {
var R = $resource(':relativePath');
$httpBackend.expect('GET', 'data.json').respond('{}');