mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-25 16:55:07 +02:00
Add XMLHttpRequest tests and examples
This commit is contained in:
parent
2dda87fe7f
commit
39e6e61a6d
3 changed files with 266 additions and 0 deletions
68
examples/Test.js
Normal file
68
examples/Test.js
Normal file
|
@ -0,0 +1,68 @@
|
|||
//
|
||||
// Test.js
|
||||
// examples
|
||||
//
|
||||
// Created by Ryan Huffman on 5/4//14
|
||||
// Copyright 2014 High Fidelity, Inc.
|
||||
//
|
||||
// This provides very basic unit testing functionality.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
test = function(name, func) {
|
||||
print("Running test: " + name);
|
||||
|
||||
var unitTest = new UnitTest(name, func);
|
||||
|
||||
try {
|
||||
unitTest.run();
|
||||
print(" Success: " + unitTest.numAssertions + " assertions passed");
|
||||
} catch (error) {
|
||||
print(" Failure: " + error.message);
|
||||
}
|
||||
};
|
||||
|
||||
AssertionException = function(expected, actual, message) {
|
||||
print("Creating exception");
|
||||
this.message = message + "\n: " + actual + " != " + expected;
|
||||
this.name = 'AssertionException';
|
||||
};
|
||||
|
||||
UnitTest = function(name, func) {
|
||||
this.numAssertions = 0;
|
||||
this.func = func;
|
||||
};
|
||||
|
||||
UnitTest.prototype.run = function() {
|
||||
this.func();
|
||||
};
|
||||
|
||||
UnitTest.prototype.assertNotEquals = function(expected, actual, message) {
|
||||
this.numAssertions++;
|
||||
if (expected == actual) {
|
||||
throw new AssertionException(expected, actual, message);
|
||||
}
|
||||
};
|
||||
|
||||
UnitTest.prototype.assertEquals = function(expected, actual, message) {
|
||||
this.numAssertions++;
|
||||
if (expected != actual) {
|
||||
throw new AssertionException(expected, actual, message);
|
||||
}
|
||||
};
|
||||
|
||||
UnitTest.prototype.assertHasProperty = function(property, actual, message) {
|
||||
this.numAssertions++;
|
||||
if (actual[property] === undefined) {
|
||||
throw new AssertionException(property, actual, message);
|
||||
}
|
||||
};
|
||||
|
||||
UnitTest.prototype.assertNull = function(value, message) {
|
||||
this.numAssertions++;
|
||||
if (value !== null) {
|
||||
throw new AssertionException(value, null, message);
|
||||
}
|
||||
};
|
51
examples/streetAreaExample.js
Normal file
51
examples/streetAreaExample.js
Normal file
|
@ -0,0 +1,51 @@
|
|||
//
|
||||
// streetAreaExample.js
|
||||
// examples
|
||||
//
|
||||
// Created by Ryan Huffman on 5/4//14
|
||||
// Copyright 2014 High Fidelity, Inc.
|
||||
//
|
||||
// This is an example script showing how to load JSON data using XMLHttpRequest.
|
||||
//
|
||||
// URL Macro created by Thijs Wenker.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
var url = "https://script.google.com/macros/s/AKfycbwIo4lmF-qUwX1Z-9eA_P-g2gse9oFhNcjVyyksGukyDDEFXgU/exec?action=listOwners&domain=alpha.highfidelity.io";
|
||||
print("Loading street data from " + url);
|
||||
|
||||
var req = new XMLHttpRequest();
|
||||
|
||||
// Set response type to "json". This will tell XMLHttpRequest to parse the response data as json, so req.response can be used
|
||||
// as a regular javascript object
|
||||
req.responseType = 'json';
|
||||
|
||||
req.open("GET", url, false);
|
||||
req.send();
|
||||
|
||||
if (req.status == 200) {
|
||||
for (var domain in req.response) {
|
||||
print("DOMAIN: " + domain);
|
||||
var locations = req.response[domain];
|
||||
var userAreas = [];
|
||||
for (var i = 0; i < locations.length; i++) {
|
||||
var loc = locations[i];
|
||||
var x1 = loc[1],
|
||||
y1 = loc[2],
|
||||
x2 = loc[3],
|
||||
y2 = loc[4];
|
||||
userAreas.push({
|
||||
username: loc[0],
|
||||
area: Math.abs(x2 - x1) * Math.abs(y2 - y1),
|
||||
});
|
||||
}
|
||||
userAreas.sort(function(a, b) { return a.area > b.area ? -1 : (a.area < b.area ? 1 : 0) });
|
||||
for (var i = 0; i < userAreas.length; i++) {
|
||||
print(userAreas[i].username + ": " + userAreas[i].area + " sq units");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
print("Error loading data: " + req.status + " " + req.statusText + ", " + req.errorCode);
|
||||
}
|
147
examples/testXMLHttpRequest.js
Normal file
147
examples/testXMLHttpRequest.js
Normal file
|
@ -0,0 +1,147 @@
|
|||
//
|
||||
// testXMLHttpRequest.js
|
||||
// examples
|
||||
//
|
||||
// Created by Ryan Huffman on 5/4//14
|
||||
// Copyright 2014 High Fidelity, Inc.
|
||||
//
|
||||
// XMLHttpRequest Tests
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
Script.include("Test.js");
|
||||
|
||||
test("Test default request values", function(finished) {
|
||||
var req = new XMLHttpRequest();
|
||||
|
||||
this.assertEquals(req.UNSENT, req.readyState, "readyState should be UNSENT");
|
||||
this.assertEquals(0, req.status, "status should be `0` by default");
|
||||
this.assertEquals("", req.statusText, "statusText should be empty string by default");
|
||||
this.assertEquals("", req.getAllResponseHeaders(), "getAllResponseHeaders() should return empty string by default");
|
||||
this.assertEquals("", req.response, "response should be empty string by default");
|
||||
this.assertEquals("", req.responseText, "responseText should be empty string by default");
|
||||
this.assertEquals("", req.responseType, "responseType should be empty string by default");
|
||||
this.assertEquals(0, req.timeout, "timeout should be `0` by default");
|
||||
this.assertEquals(0, req.errorCode, "there should be no error by default");
|
||||
});
|
||||
|
||||
|
||||
test("Test readyStates", function() {
|
||||
var req = new XMLHttpRequest();
|
||||
var state = req.readyState;
|
||||
|
||||
var statesVisited = [true, false, false, false, false]
|
||||
|
||||
req.onreadystatechange = function() {
|
||||
statesVisited[req.readyState] = true;
|
||||
};
|
||||
|
||||
req.open("GET", "https://gist.githubusercontent.com/huffman/33cc618fec183d1bccd0/raw/test.json", false);
|
||||
req.send();
|
||||
for (var i = 0; i <= req.DONE; i++) {
|
||||
this.assertEquals(true, statesVisited[i], i + " should be set");
|
||||
}
|
||||
this.assertEquals(req.DONE, req.readyState, "readyState should be DONE");
|
||||
});
|
||||
|
||||
test("Test TEXT request", function() {
|
||||
var req = new XMLHttpRequest();
|
||||
var state = req.readyState;
|
||||
|
||||
req.open("GET", "https://gist.githubusercontent.com/huffman/33cc618fec183d1bccd0/raw/test.json", false);
|
||||
req.send();
|
||||
|
||||
this.assertEquals(req.DONE, req.readyState, "readyState should be DONE");
|
||||
this.assertEquals(200, req.status, "status should be `200`");
|
||||
this.assertEquals(0, req.errorCode);
|
||||
this.assertEquals("OK", req.statusText, "statusText should be `OK`");
|
||||
this.assertNotEquals("", req.getAllResponseHeaders(), "headers should no longer be empty string");
|
||||
this.assertNull(req.getResponseHeader('invalidheader'), "invalid header should return `null`");
|
||||
this.assertEquals("GitHub.com", req.getResponseHeader('Server'), "Server header should be GitHub.com");
|
||||
this.assertEquals('{"id": 1}', req.response);
|
||||
this.assertEquals('{"id": 1}', req.responseText);
|
||||
});
|
||||
|
||||
test("Test JSON request", function() {
|
||||
var req = new XMLHttpRequest();
|
||||
var state = req.readyState;
|
||||
|
||||
req.responseType = "json";
|
||||
req.open("GET", "https://gist.githubusercontent.com/huffman/33cc618fec183d1bccd0/raw/test.json", false);
|
||||
req.send();
|
||||
|
||||
this.assertEquals(req.DONE, req.readyState, "readyState should be DONE");
|
||||
this.assertEquals(200, req.status, "status should be `200`");
|
||||
this.assertEquals(0, req.errorCode);
|
||||
this.assertEquals("OK", req.statusText, "statusText should be `OK`");
|
||||
this.assertNotEquals("", req.getAllResponseHeaders(), "headers should no longer be empty string");
|
||||
this.assertNull(req.getResponseHeader('invalidheader'), "invalid header should return `null`");
|
||||
this.assertEquals("GitHub.com", req.getResponseHeader('Server'), "Server header should be GitHub.com");
|
||||
this.assertHasProperty('id', req.response);
|
||||
this.assertEquals(1, req.response.id);
|
||||
this.assertEquals('{"id": 1}', req.responseText);
|
||||
});
|
||||
|
||||
test("Test Bad URL", function() {
|
||||
var req = new XMLHttpRequest();
|
||||
var state = req.readyState;
|
||||
|
||||
req.open("POST", "hifi://domain/path", false);
|
||||
req.send();
|
||||
|
||||
this.assertEquals(req.DONE, req.readyState, "readyState should be DONE");
|
||||
this.assertNotEquals(0, req.errorCode);
|
||||
});
|
||||
|
||||
test("Test Bad Method Error", function() {
|
||||
var req = new XMLHttpRequest();
|
||||
var state = req.readyState;
|
||||
|
||||
req.open("POST", "https://www.google.com", false);
|
||||
|
||||
req.send("randomdata");
|
||||
|
||||
this.assertEquals(req.DONE, req.readyState, "readyState should be DONE");
|
||||
this.assertEquals(405, req.status);
|
||||
this.assertEquals(202, req.errorCode);
|
||||
|
||||
req.open("POST", "https://www.google.com", false)
|
||||
req.send();
|
||||
|
||||
this.assertEquals(req.DONE, req.readyState, "readyState should be DONE");
|
||||
this.assertEquals(405, req.status);
|
||||
this.assertEquals(202, req.errorCode);
|
||||
});
|
||||
|
||||
test("Test abort", function() {
|
||||
var req = new XMLHttpRequest();
|
||||
var state = req.readyState;
|
||||
|
||||
req.open("POST", "https://www.google.com", true)
|
||||
req.send();
|
||||
req.abort();
|
||||
|
||||
this.assertEquals(0, req.status);
|
||||
this.assertEquals(0, req.errorCode);
|
||||
});
|
||||
|
||||
test("Test timeout", function() {
|
||||
var req = new XMLHttpRequest();
|
||||
var state = req.readyState;
|
||||
var timedOut = false;
|
||||
|
||||
req.ontimeout = function() {
|
||||
timedOut = true;
|
||||
};
|
||||
|
||||
req.open("POST", "https://gist.githubusercontent.com/huffman/33cc618fec183d1bccd0/raw/test.json", false)
|
||||
req.timeout = 1;
|
||||
req.send();
|
||||
|
||||
this.assertEquals(true, timedOut, "request should have timed out");
|
||||
this.assertEquals(req.DONE, req.readyState, "readyState should be DONE");
|
||||
this.assertEquals(0, req.status, "status should be `0`");
|
||||
this.assertEquals(4, req.errorCode, "4 is the timeout error code for QNetworkReply::NetworkError");
|
||||
});
|
Loading…
Reference in a new issue