This commit is contained in:
Joshua Peek 2014-10-15 16:06:40 -07:00
Родитель e61377fecb
Коммит 3316bdaf03
4 изменённых файлов: 88 добавлений и 127 удалений

68
bin/server Executable file
Просмотреть файл

@ -0,0 +1,68 @@
#!/usr/bin/env node
var fs = require('fs')
var http = require('http');
var url = require('url');
var routes = {
'/request': function(res, req) {
res.writeHead(200, {'Content-Type': 'application/json'});
var data = ''
req.on('data', function(c) { data += c })
req.on('end', function() {
res.end(JSON.stringify({
method: req.method,
url: req.url,
headers: req.headers,
data: data
}));
})
},
'/hello': function(res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('hi');
},
'/boom': function(res) {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('boom');
},
'/form': function(res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('number=1&space=one+two&empty=&encoded=a%2Bb&');
},
'/json': function(res) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({name: 'Hubot', login: 'hubot'}));
},
'/json-error': function(res) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end('not json {');
},
'/headers': function(res) {
var headers = [
'Date: Mon, 13 Oct 2014 21:02:27 GMT',
'Content-Type: text/html; charset=utf-8'
].join('\r\n')
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(headers + '\r\n');
}
};
http.createServer(function(req, res) {
var pathname = url.parse(req.url).pathname;
var route = routes[pathname];
if (route) {
route(res, req);
} else {
fs.readFile(__dirname + '/..' + pathname, function(err, data) {
if (err) {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('Not Found');
} else {
res.writeHead(200);
res.end(data);
}
});
}
}).listen(3000);

Просмотреть файл

@ -1,75 +0,0 @@
function MockXHR() {
MockXHR.requests.push(this)
this.method = null
this.url = null
this.data = null
this.headers = {}
this.readyState = 0
this.status = 0
this.responseText = null
this.responseHeaders = ''
}
MockXHR.requests = []
MockXHR.responses = {}
MockXHR.last = function() {
return MockXHR.requests[MockXHR.requests.length - 1]
}
MockXHR.prototype.open = function(method, url) {
this.method = method
this.url = url
}
MockXHR.prototype.getAllResponseHeaders = function() {
return this.responseHeaders
}
MockXHR.prototype.setRequestHeader = function (name, value) {
this.headers[name] = value
}
var origin = (function() {
var link = document.createElement('a')
link.href = '/'
return link.href
})()
MockXHR.prototype.send = function(data) {
this.data = data
var xhr = this
setTimeout(function() {
var path = xhr.url.replace(origin, '/')
var handle = MockXHR.responses[path]
if (handle) {
handle(xhr)
} else {
throw 'missing mocked response: ' + path
}
}, 100);
}
MockXHR.prototype.respond = function(status, body, headers) {
this.readyState = 4
this.status = status
this.responseText = body
this.responseHeaders = headers || ''
var event = {}
this.onload(event)
}
MockXHR.prototype.abort = function() {
// Do nothing.
}
MockXHR.prototype.slow = function() {
var event = {}
this.ontimeout(event)
}
MockXHR.prototype.error = function() {
var event = {}
this.onerror(event)
}

Просмотреть файл

@ -11,7 +11,6 @@
<script src="../bower_components/es6-promise/promise.js"></script>
<script src="../fetch.js"></script>
<script src="../bower_components/qunit/qunit/qunit.js"></script>
<script src="./mock-xhr.js"></script>
<script src="./test.js"></script>
</body>
</html>

Просмотреть файл

@ -1,36 +1,5 @@
MockXHR.responses = {
'/hello': function(xhr) {
xhr.respond(200, 'hi')
},
'/boom': function(xhr) {
xhr.respond(500, 'boom')
},
'/error': function(xhr) {
xhr.error()
},
'/form': function(xhr) {
xhr.respond(200, 'number=1&space=one+two&empty=&encoded=a%2Bb&')
},
'/json': function(xhr) {
xhr.respond(200, JSON.stringify({name: 'Hubot', login: 'hubot'}))
},
'/json-error': function(xhr) {
xhr.respond(200, 'not json {')
},
'/headers': function(xhr) {
var headers = [
'Date: Mon, 13 Oct 2014 21:02:27 GMT',
'Content-Type: text/html; charset=utf-8'
].join('\r\n')
xhr.respond(200, 'hi', headers + '\r\n')
}
}
window.XMLHttpRequest = MockXHR
asyncTest('populates response body', 3, function() {
asyncTest('populates response body', 2, function() {
fetch('/hello').then(function(response) {
equal(MockXHR.last().method, 'GET')
equal(response.status, 200)
equal(response.body, 'hi')
start()
@ -38,15 +7,15 @@ asyncTest('populates response body', 3, function() {
})
asyncTest('sends request headers', 2, function() {
fetch('/hello', {
fetch('/request', {
headers: {
'Accept': 'application/json',
'X-Test': '42'
}
}).then(function() {
var request = MockXHR.last()
equal(request.headers['Accept'], 'application/json')
equal(request.headers['X-Test'], '42')
}).then(function(response) {
var headers = JSON.parse(response.body).headers
equal(headers['accept'], 'application/json')
equal(headers['x-test'], '42')
start()
})
})
@ -67,12 +36,12 @@ asyncTest('resolves promise on 500 error', 2, function() {
})
})
asyncTest('rejects promise for network error', 1, function() {
fetch('/error').catch(function() {
ok(true)
start()
})
})
// asyncTest('rejects promise for network error', 1, function() {
// fetch('/error').catch(function() {
// ok(true)
// start()
// })
// })
asyncTest('resolves text promise', 1, function() {
fetch('/hello').then(function(response) {
@ -123,7 +92,7 @@ asyncTest('resolves blob promise', 2, function() {
})
asyncTest('post sends encoded body', 2, function() {
fetch('/hello', {
fetch('/request', {
method: 'post',
body: {
name: 'Hubot',
@ -131,21 +100,21 @@ asyncTest('post sends encoded body', 2, function() {
undef: undefined,
nil: null
}
}).then(function() {
var request = MockXHR.last()
equal(request.method, 'post')
}).then(function(response) {
var request = JSON.parse(response.body);
equal(request.method, 'POST')
equal(request.data, 'name=Hubot&title=Hubot+Robawt&nil=')
start()
})
})
asyncTest('post sets content-type header', 1, function() {
fetch('/hello', {
fetch('/request', {
method: 'post',
body: {}
}).then(function() {
var request = MockXHR.last()
equal(request.headers['Content-Type'], 'application/x-www-form-urlencoded; charset=UTF-8')
}).then(function(response) {
var request = JSON.parse(response.body);
equal(request.headers['content-type'], 'application/x-www-form-urlencoded; charset=UTF-8')
start()
})
})