OpenLayers OpenLayers

Changeset 7535

Show
Ignore:
Timestamp:
07/17/08 09:58:18 (5 months ago)
Author:
elemoine
Message:

improve HTTP protocol test coverage

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/vector-behavior/tests/Protocol/HTTP.html

    r7489 r7535  
    5555        var headers = {'k': 'bar_header'}; 
    5656        var scope = {'hello': 'world'}; 
     57 
    5758        var callback = function(resp) { 
    5859            // 4 tests 
     
    9293        var protocol = new OpenLayers.Protocol.HTTP({ 
    9394            'url': 'foo_url', 
    94             'params': {'k': 'foo_param'} 
    95         }); 
    96  
    97         // fake XHR request object 
    98         var request = {'status': 200}; 
    99  
    100         var url = 'bar_url'; 
    101         var params = {'k': 'bar_param'}; 
    102         var headers = {'k': 'bar_header'}; 
    103         var scope = {'hello': 'world'}; 
    104         var callback = function(resp) { 
    105             // 4 tests 
    106             t.ok(this == scope, 
    107                 'callback is called with the correct scope'); 
    108             t.eq(resp.CLASS_NAME, 'OpenLayers.Protocol.Response', 
    109                 'callback is passed a Response object'); 
    110             t.ok(resp.priv == request, 
    111                 'callback is passed the request in the Response object'); 
    112             t.eq(resp.code, OpenLayers.Protocol.Response.SUCCESS, 
    113                 'callback is passed the correct code in the Response object'); 
     95            'format': {'write': function() {}} 
     96        }); 
     97 
     98        // fake XHR request object 
     99        var request = {'status': 200}; 
     100 
     101        var url = 'bar_url'; 
     102        var headers = {'k': 'bar_header'}; 
     103        var scope = {'hello': 'world'}; 
     104        var features = ['feature']; 
     105 
     106        var callback = function(resp) { 
     107            // 5 tests 
     108            t.ok(this == scope, 
     109                'callback is called with the correct scope'); 
     110            t.eq(resp.CLASS_NAME, 'OpenLayers.Protocol.Response', 
     111                'callback is passed a Response object'); 
     112            t.ok(resp.priv == request, 
     113                'callback is passed the request in the Response object'); 
     114            t.eq(resp.code, OpenLayers.Protocol.Response.SUCCESS, 
     115                'callback is passed the correct code in the Response object'); 
     116            t.ok(resp.reqFeatures == features, 
     117                'callback is passed the correct to-create features in the Response object'); 
    114118        }; 
    115119 
     
    117121 
    118122        OpenLayers.Request.POST = function(options) { 
    119             // 5 tests 
     123            // 4 tests 
    120124            t.eq(options.url, url, 'protocol url overriden by create url'); 
    121             t.eq(options.params['k'], params['k'], 'protocol params overriden by create params'); 
    122             t.eq(options.headers['k'], headers['k'], 'protocol headers correctly sets'); 
    123             t.ok(typeof options.callback == 'function', 'create passes a callback function to Request.GET'); 
    124             t.ok(options.scope == protocol, 'create passed correct scope to Request.GET'); 
    125             // call callback 
    126             options.callback.call(options.scope, request); 
    127         }; 
    128         protocol.create({ 
    129             'url': url, 'params': params, 'headers': headers, 'callback': callback, 'scope': scope 
     125            t.eq(options.headers['k'], headers['k'], 'protocol headers correctly sets'); 
     126            t.ok(typeof options.callback == 'function', 'create passes a callback function to Request.POST'); 
     127            t.ok(options.scope == protocol, 'create passed correct scope to Request.POST'); 
     128            // call callback 
     129            options.callback.call(options.scope, request); 
     130        }; 
     131        protocol.create(features, { 
     132            'url': url, 'headers': headers, 'callback': callback, 'scope': scope 
    130133        }); 
    131134 
     
    133136        protocol.destroy(); 
    134137        OpenLayers.Request.POST = _post; 
     138    } 
     139 
     140    function test_Protocol_HTTP_update(t) { 
     141        t.plan(9); 
     142        var protocol = new OpenLayers.Protocol.HTTP({ 
     143            'url': 'foo_url', 
     144            'format': {'write': function() {}} 
     145        }); 
     146 
     147        // fake XHR request object 
     148        var request = {'status': 200}; 
     149 
     150        var url = 'bar_url'; 
     151        var headers = {'k': 'bar_header'}; 
     152        var scope = {'hello': 'world'}; 
     153        var feature = {'feature':'feature'}; 
     154 
     155        var callback = function(resp) { 
     156            // 5 tests 
     157            t.ok(this == scope, 
     158                'callback is called with the correct scope'); 
     159            t.eq(resp.CLASS_NAME, 'OpenLayers.Protocol.Response', 
     160                'callback is passed a Response object'); 
     161            t.ok(resp.priv == request, 
     162                'callback is passed the request in the Response object'); 
     163            t.eq(resp.code, OpenLayers.Protocol.Response.SUCCESS, 
     164                'callback is passed the correct code in the Response object'); 
     165            t.ok(resp.reqFeatures == feature, 
     166                'callback is passed the correct to-update feature in the Response object'); 
     167        }; 
     168 
     169        var _put = OpenLayers.Request.PUT; 
     170 
     171        OpenLayers.Request.PUT = function(options) { 
     172            // 4 tests 
     173            t.eq(options.url, url, 'protocol url overriden by create url'); 
     174            t.eq(options.headers['k'], headers['k'], 'protocol headers correctly sets'); 
     175            t.ok(typeof options.callback == 'function', 'create passes a callback function to Request.PUT'); 
     176            t.ok(options.scope == protocol, 'create passed correct scope to Request.PUT'); 
     177            // call callback 
     178            options.callback.call(options.scope, request); 
     179        }; 
     180        protocol.update(feature, { 
     181            'url': url, 'headers': headers, 'callback': callback, 'scope': scope 
     182        }); 
     183 
     184        // cleanup 
     185        protocol.destroy(); 
     186        OpenLayers.Request.PUT = _put; 
     187    } 
     188 
     189    function test_Protocol_HTTP_delete(t) { 
     190        t.plan(9); 
     191        var protocol = new OpenLayers.Protocol.HTTP({ 
     192            'url': 'foo_url' 
     193        }); 
     194 
     195        // fake XHR request object 
     196        var request = {'status': 200}; 
     197 
     198        var url = 'bar_url'; 
     199        var headers = {'k': 'bar_header'}; 
     200        var scope = {'hello': 'world'}; 
     201        var feature = {'url': url}; 
     202 
     203        var callback = function(resp) { 
     204            // 5 tests 
     205            t.ok(this == scope, 
     206                'callback is called with the correct scope'); 
     207            t.eq(resp.CLASS_NAME, 'OpenLayers.Protocol.Response', 
     208                'callback is passed a Response object'); 
     209            t.ok(resp.priv == request, 
     210                'callback is passed the request in the Response object'); 
     211            t.eq(resp.code, OpenLayers.Protocol.Response.SUCCESS, 
     212                'callback is passed the correct code in the Response object'); 
     213            t.ok(resp.reqFeatures == feature, 
     214                'callback is passed the correct to-delete feature in the Response object'); 
     215        }; 
     216 
     217        var _delete = OpenLayers.Request.DELETE; 
     218 
     219        OpenLayers.Request.DELETE = function(options) { 
     220            // 4 tests 
     221            t.eq(options.url, url, 'protocol url overriden by create url'); 
     222            t.eq(options.headers['k'], headers['k'], 'protocol headers correctly sets'); 
     223            t.ok(typeof options.callback == 'function', 'create passes a callback function to Request.DELETE'); 
     224            t.ok(options.scope == protocol, 'create passed correct scope to Request.DELETE'); 
     225            // call callback 
     226            options.callback.call(options.scope, request); 
     227        }; 
     228        protocol.delete(feature, { 
     229            'headers': headers, 'callback': callback, 'scope': scope 
     230        }); 
     231 
     232        // cleanup 
     233        protocol.destroy(); 
     234        OpenLayers.Request.DELETE = _delete; 
    135235    } 
    136236