Skip to content
This repository was archived by the owner on Apr 20, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/operators/ajax.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ OR
- `method` *(String)*: Method of the request, such as GET, POST, PUT, PATCH, DELETE. The default is GET.
- `password` *(String)*: The password for the request.
- `progressObserver` *(Observer)*: An optional `Observer` which listen to XHR2 progress events or error timeout values.
- `responseType` *(String)*: The response type. Either can be 'json', 'text' or 'blob'. The default is 'text'
- `responseType` *(String)*: The response type. Either can be 'json', 'text', 'blob' or 'arraybuffer'. The default is 'text'
- `timeout`: `Number` - a number representing the number of milliseconds a request can take before automatically being terminated. A value of 0 (which is the default) means there is no timeout.
- `url` *(String)*: URL of the request
- `user` *(String)*: The user for the request.
Expand Down
10 changes: 4 additions & 6 deletions src/ajax/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
}
}

function normalizeAjaxSuccessEvent(e, xhr, settings) {
function normalizeAjaxSuccessEvent(e, xhr) {
var response = ('response' in xhr) ? xhr.response : xhr.responseText;
response = settings.responseType === 'json' ? JSON.parse(response) : response;
response = xhr.responseType === 'json' && response && typeof response === 'string' ? JSON.parse(response) : response;
return {
response: response,
status: xhr.status,
Expand Down Expand Up @@ -74,7 +74,7 @@
var processResponse = function(xhr, e){
var status = xhr.status === 1223 ? 204 : xhr.status;
if ((status >= 200 && status <= 300) || status === 0 || status === '') {
o.onNext(normalizeSuccess(e, xhr, settings));
o.onNext(normalizeSuccess(e, xhr));
o.onCompleted();
} else {
o.onError(settings.normalizeError(e, xhr, 'error'));
Expand All @@ -95,9 +95,7 @@
xhr.open(settings.method, settings.url, settings.async);
}

if (settings.responseType === 'blob') {
xhr.responseType = 'blob';
}
xhr.responseType = settings.responseType;

var headers = settings.headers;
for (var header in headers) {
Expand Down
95 changes: 91 additions & 4 deletions tests/ajax/tests.ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,92 @@
requests[0].respond(500, { 'Content-Type': 'application/json' }, 'error');
});

test('ajax arraybuffer success', function () {
var source = Rx.DOM.ajax({
url: '/products/1/image',
method: 'GET',
responseType: 'arraybuffer',
headers: {
'X-Requested-With': 'RxJS',
'Accept': 'application/octet-stream'
}
});

source.subscribe(
function (x) {
// Ensure GET by default
equal('GET', x.xhr.method);

// Ensure status
equal(200, x.status);

// Ensure async
ok(x.xhr.async);

//Assert equality for the message
ok(x.xhr.response instanceof ArrayBuffer);

var data = '';
var bytes = new Uint8Array(x.xhr.response);
for (var i = 0; i < bytes.byteLength; i++) {
data += String.fromCharCode(bytes[i]);
}
equal('a test array buffer', data);
},
function () {
ok(false);
},
function () {
ok(true);
}
);

requests[0].respond(200, { 'Content-Type': 'application/octet-stream' }, 'a test array buffer');
});

asyncTest('ajax blob success', function () {
var source = Rx.DOM.ajax({
url: '/products/2/image',
method: 'GET',
responseType: 'blob',
headers: {
'X-Requested-With': 'RxJS',
'Accept': 'application/octet-stream'
}
});

source.subscribe(
function (x) {
// Ensure GET by default
equal('GET', x.xhr.method);

// Ensure status
equal(200, x.status);

// Ensure async
ok(x.xhr.async);

//Assert equality for the message
ok(x.xhr.response instanceof Blob);

var blobReader = new FileReader();
blobReader.onloadend = function () {
start();
equal('a test blob', blobReader.result);
};
blobReader.readAsText(x.xhr.response);
},
function () {
ok(false);
},
function () {
ok(true);
}
);

requests[0].respond(200, { 'Content-Type': 'application/octet-stream' }, 'a test blob');
});

test('ajax failure settings', function () {
var source = Rx.DOM.ajax({
url: '/products',
Expand Down Expand Up @@ -442,7 +528,7 @@
}
);

requests[0].respond(500, { 'Content-Type': 'application/json' }, 'error');
requests[0].respond(500, { 'Content-Type': 'text/plain' }, 'error');
});


Expand All @@ -464,6 +550,7 @@
requests[0].respond(200, { 'Content-Type': 'application/json' }, '[{ "id": 123 }]');
});


test('getJSON failure', function () {
var source = Rx.DOM.getJSON('/products');

Expand All @@ -472,7 +559,7 @@
// Should not happen
ok(false);
},
function (x) {
function (x) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you noticed those spaces?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I didn't, sorry about that will get that fixed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

// Ensure GET by default
equal('GET', x.xhr.method);

Expand All @@ -483,7 +570,7 @@
ok(x.xhr.async);

// Assert equality for the message
equal('error', x.xhr.responseText);
ok(x.xhr.response.error);

// Assert type of error
equal('error', x.type);
Expand All @@ -493,7 +580,7 @@
}
);

requests[0].respond(500, { 'Content-Type': 'application/json' }, 'error');
requests[0].respond(500, { 'Content-Type': 'application/json' }, '{ "error": true }');
});

}());
2 changes: 1 addition & 1 deletion tests/rx.dom.compat.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<body>
<div id="qunit"></div>
<script src="vendor/qunit-1.9.0.js"></script>
<script src="vendor/sinon-1.7.3.js"></script>
<script src="vendor/sinon-1.17.3.js"></script>
<script src="../node_modules/rx/dist/rx.lite.compat.js"></script>
<script src="../node_modules/rx/dist/rx.virtualtime.js"></script>
<script src="../node_modules/rx/dist/rx.testing.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion tests/rx.dom.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
}
</script>
<script src="vendor/qunit-1.9.0.js"></script>
<script src="vendor/sinon-1.7.3.js"></script>
<script src="vendor/sinon-1.17.3.js"></script>
<script src="../node_modules/rx/dist/rx.lite.js"></script>
<script src="../node_modules/rx/dist/rx.virtualtime.js"></script>
<script src="../node_modules/rx/dist/rx.testing.js"></script>
Expand Down
Loading