      1 pockets.test.AjaxRequests = {
      2   requests: $A(),
      3
      4   activeRequest: function() {
      5     return this.requests.last();
      6   },
      7
      8   add: function(request) {
      9     this.requests.push(request);
     10     var spec = jasmine.getEnv().currentSpec;
     11     spec.after(function() { pockets.test.AjaxRequests.clear(); });
     12   },
     13
     14   remove: function(request) {
     15     this.requests = this.requests.without(request);
     16   },
     17
     18   clear: function() {
     19     this.requests.clear();
     20   }
     21 };
     22
     23 pockets.test.AjaxRequest = Class.create(Ajax.Request, {
     24   request: function(url) {
     25     this.url = url;
     26     this.method = this.options.method;
     27     var params = Object.clone(this.options.parameters);
     28
     29     if (!['get', 'post'].include(this.method)) {
     30       // simulate other verbs over post
     31       params['_method'] = this.method;
     32       this.method = 'post';
     33     }
     34
     35     this.parameters = params;
     36
     37     if (params = Object.toQueryString(params)) {
     38       // when GET, append parameters to URL
     39       if (this.method == 'get')
     40         this.url += (this.url.include('?') ? '&' : '?') + params;
     41       else if (/Konqueror|Safari|KHTML/.test(navigator.userAgent))
     42         params += '&_=';
     43     }
     44
     45     pockets.test.AjaxRequests.add(this);
     46   },
     47
     48   response: function(response) {
     49     if (!response.status || response.status == 200 || response.status == "200") {
     50       if (this.options.onSuccess) {this.options.onSuccess(response);}
     51     } else {
     52       if (this.options.onFailure) {this.options.onFailure(response);}
     53     }
     54     if (this.options.onComplete) {this.options.onComplete(response);}
     55     pockets.test.AjaxRequests.remove(this);
     56   }
     57 });