Last active
August 29, 2015 14:27
-
-
Save blink1073/f4d8d0d2b102dcc1db09 to your computer and use it in GitHub Desktop.
Node Ajax tester
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Type definitions for faux-jax | |
// Project: https://github.com/algolia/faux-jax | |
declare module 'faux-jax' { | |
export function install(): void; | |
export function on(type: string, cb: (xhr: any) => void): void; | |
export function waitFor(nbRequests: number, cb: () => void): void; | |
export function restore(): void; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) Jupyter Development Team. | |
// Distributed under the terms of the Modified BSD License. | |
import * as fauxJax from 'faux-jax'; | |
import {XMLHttpRequest} from "xmlhttprequest"; | |
export | |
class RequestHandler { | |
/** | |
* Create a new RequestHandler. | |
*/ | |
constructor() { | |
fauxJax.on('request', (xhr: any) => { | |
this._requests.push(xhr); | |
}); | |
} | |
/** | |
* Respond to the latest Ajax request. | |
*/ | |
respond(statusCode: number, data: any, header?: any): void { | |
if (typeof data !== 'string') { | |
data = JSON.stringify(data); | |
} | |
if (header === void 0) { | |
header = {'Content-Type': 'text/json'}; | |
} | |
this._requests[this._requests.length - 1].respond(statusCode, header, data); | |
} | |
private _requests: any[] = []; | |
private _xhr: any = null; | |
} | |
describe('jupyter.services - test_utils', () => { | |
it('should handle a request', (done) => { | |
var handler = new RequestHandler(); | |
console.log('handler:', handler); | |
var xmlhttp = new XMLHttpRequest(); | |
var url = "http://hello.com"; | |
xmlhttp.onreadystatechange = function() { | |
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { | |
done(); | |
} | |
} | |
xmlhttp.open("GET", url, true); | |
xmlhttp.send(); | |
handler.respond(200, ''); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Type definitions for faux-jax | |
// Project: https://github.com/algolia/faux-jax | |
declare module 'xmlhttprequest' { | |
export class XMLHttpRequest { | |
onreadystatechange(cb: any): void; | |
readyState: number; | |
status: number; | |
open(method: string, url: string, cache: boolean): void; | |
send(): void; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment