finally works
This commit is contained in:
-6
@@ -1,6 +0,0 @@
|
||||
FAILS=0
|
||||
for i in tests/test-*.js; do
|
||||
echo $i
|
||||
node $i || let FAILS++
|
||||
done
|
||||
exit $FAILS
|
||||
+40
-7
@@ -1,4 +1,7 @@
|
||||
var http = require('http')
|
||||
var fs = require('fs')
|
||||
, http = require('http')
|
||||
, path = require('path')
|
||||
, https = require('https')
|
||||
, events = require('events')
|
||||
, stream = require('stream')
|
||||
, assert = require('assert')
|
||||
@@ -14,6 +17,28 @@ exports.createServer = function (port) {
|
||||
return s;
|
||||
}
|
||||
|
||||
exports.createSSLServer = function(port, opts) {
|
||||
port = port || 16767
|
||||
|
||||
var options = { 'key' : path.join(__dirname, 'ssl', 'test.key')
|
||||
, 'cert': path.join(__dirname, 'ssl', 'test.crt')
|
||||
}
|
||||
if (opts) {
|
||||
for (var i in opts) options[i] = opts[i]
|
||||
}
|
||||
|
||||
for (var i in options) {
|
||||
options[i] = fs.readFileSync(options[i])
|
||||
}
|
||||
|
||||
var s = https.createServer(options, function (req, resp) {
|
||||
s.emit(req.url, req, resp);
|
||||
})
|
||||
s.port = port
|
||||
s.url = 'https://localhost:'+port
|
||||
return s;
|
||||
}
|
||||
|
||||
exports.createPostStream = function (text) {
|
||||
var postStream = new stream.Stream();
|
||||
postStream.writeable = true;
|
||||
@@ -21,16 +46,24 @@ exports.createPostStream = function (text) {
|
||||
setTimeout(function () {postStream.emit('data', new Buffer(text)); postStream.emit('end')}, 0);
|
||||
return postStream;
|
||||
}
|
||||
exports.createPostValidator = function (text) {
|
||||
exports.createPostValidator = function (text, reqContentType) {
|
||||
var l = function (req, resp) {
|
||||
var r = '';
|
||||
req.on('data', function (chunk) {r += chunk})
|
||||
req.on('end', function () {
|
||||
if (r !== text) console.log(r, text);
|
||||
assert.equal(r, text)
|
||||
resp.writeHead(200, {'content-type':'text/plain'})
|
||||
resp.write('OK')
|
||||
resp.end()
|
||||
if (req.headers['content-type'] && req.headers['content-type'].indexOf('boundary=') >= 0) {
|
||||
var boundary = req.headers['content-type'].split('boundary=')[1];
|
||||
text = text.replace(/__BOUNDARY__/g, boundary);
|
||||
}
|
||||
if (r !== text) console.log(r, text);
|
||||
assert.equal(r, text)
|
||||
if (reqContentType) {
|
||||
assert.ok(req.headers['content-type'])
|
||||
assert.ok(~req.headers['content-type'].indexOf(reqContentType))
|
||||
}
|
||||
resp.writeHead(200, {'content-type':'text/plain'})
|
||||
resp.write('OK')
|
||||
resp.end()
|
||||
})
|
||||
}
|
||||
return l;
|
||||
|
||||
+34
-7
@@ -25,11 +25,21 @@ var tests =
|
||||
])
|
||||
, expectBody: "Ω☃"
|
||||
}
|
||||
, testGetJSON :
|
||||
{ resp : server.createGetResponse('{"test":true}', 'application/json')
|
||||
, json : true
|
||||
, expectBody: {"test":true}
|
||||
, testGetBuffer :
|
||||
{ resp : server.createGetResponse(new Buffer("TESTING!"))
|
||||
, encoding: null
|
||||
, expectBody: new Buffer("TESTING!")
|
||||
}
|
||||
, testGetEncoding :
|
||||
{ resp : server.createGetResponse(new Buffer('efa3bfcea9e29883', 'hex'))
|
||||
, encoding: 'hex'
|
||||
, expectBody: "efa3bfcea9e29883"
|
||||
}
|
||||
, testGetJSON :
|
||||
{ resp : server.createGetResponse('{"test":true}', 'application/json')
|
||||
, json : true
|
||||
, expectBody: {"test":true}
|
||||
}
|
||||
, testPutString :
|
||||
{ resp : server.createPostValidator("PUTTINGDATA")
|
||||
, method : "PUT"
|
||||
@@ -47,13 +57,13 @@ var tests =
|
||||
}
|
||||
, testPutMultipart :
|
||||
{ resp: server.createPostValidator(
|
||||
'--frontier\r\n' +
|
||||
'--__BOUNDARY__\r\n' +
|
||||
'content-type: text/html\r\n' +
|
||||
'\r\n' +
|
||||
'<html><body>Oh hi.</body></html>' +
|
||||
'\r\n--frontier\r\n\r\n' +
|
||||
'\r\n--__BOUNDARY__\r\n\r\n' +
|
||||
'Oh hi.' +
|
||||
'\r\n--frontier--'
|
||||
'\r\n--__BOUNDARY__--'
|
||||
)
|
||||
, method: "PUT"
|
||||
, multipart:
|
||||
@@ -61,6 +71,23 @@ var tests =
|
||||
, {'body': 'Oh hi.'}
|
||||
]
|
||||
}
|
||||
, testPutMultipartPreambleCRLF :
|
||||
{ resp: server.createPostValidator(
|
||||
'\r\n--__BOUNDARY__\r\n' +
|
||||
'content-type: text/html\r\n' +
|
||||
'\r\n' +
|
||||
'<html><body>Oh hi.</body></html>' +
|
||||
'\r\n--__BOUNDARY__\r\n\r\n' +
|
||||
'Oh hi.' +
|
||||
'\r\n--__BOUNDARY__--'
|
||||
)
|
||||
, method: "PUT"
|
||||
, preambleCRLF: true
|
||||
, multipart:
|
||||
[ {'content-type': 'text/html', 'body': '<html><body>Oh hi.</body></html>'}
|
||||
, {'body': 'Oh hi.'}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
s.listen(s.port, function () {
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
var Cookie = require('../vendor/cookie')
|
||||
, assert = require('assert');
|
||||
|
||||
var str = 'sid=s543qactge.wKE61E01Bs%2BKhzmxrwrnug; path=/; httpOnly; expires=Sat, 04 Dec 2010 23:27:28 GMT';
|
||||
var str = 'Sid="s543qactge.wKE61E01Bs%2BKhzmxrwrnug="; Path=/; httpOnly; Expires=Sat, 04 Dec 2010 23:27:28 GMT';
|
||||
var cookie = new Cookie(str);
|
||||
|
||||
// test .toString()
|
||||
@@ -14,10 +14,10 @@ assert.equal(cookie.path, '/');
|
||||
assert.equal(cookie.httpOnly, true);
|
||||
|
||||
// test .name
|
||||
assert.equal(cookie.name, 'sid');
|
||||
assert.equal(cookie.name, 'Sid');
|
||||
|
||||
// test .value
|
||||
assert.equal(cookie.value, 's543qactge.wKE61E01Bs%2BKhzmxrwrnug');
|
||||
assert.equal(cookie.value, '"s543qactge.wKE61E01Bs%2BKhzmxrwrnug="');
|
||||
|
||||
// test .expires
|
||||
assert.equal(cookie.expires instanceof Date, true);
|
||||
|
||||
+7
@@ -27,4 +27,11 @@ try {
|
||||
assert.equal(e.message, 'Body attribute missing in multipart.')
|
||||
}
|
||||
|
||||
try {
|
||||
request(local, {multipart: [{}]})
|
||||
assert.fail("Should have throw")
|
||||
} catch(e) {
|
||||
assert.equal(e.message, 'Body attribute missing in multipart.')
|
||||
}
|
||||
|
||||
console.log("All tests passed.")
|
||||
|
||||
+18
-10
@@ -6,7 +6,7 @@ var hmacsign = require('../oauth').hmacsign
|
||||
|
||||
function getsignature (r) {
|
||||
var sign
|
||||
r.headers.authorization.slice('OAuth '.length).replace(/,\ /g, ',').split(',').forEach(function (v) {
|
||||
r.headers.Authorization.slice('OAuth '.length).replace(/,\ /g, ',').split(',').forEach(function (v) {
|
||||
if (v.slice(0, 'oauth_signature="'.length) === 'oauth_signature="') sign = v.slice('oauth_signature="'.length, -1)
|
||||
})
|
||||
return decodeURIComponent(sign)
|
||||
@@ -56,7 +56,7 @@ console.log('yOahq5m0YjDDjfjxHaXEsW9D+X0=')
|
||||
assert.equal(upsign, 'yOahq5m0YjDDjfjxHaXEsW9D+X0=')
|
||||
|
||||
|
||||
var r = request.post(
|
||||
var rsign = request.post(
|
||||
{ url: 'https://api.twitter.com/oauth/request_token'
|
||||
, oauth:
|
||||
{ callback: 'http://localhost:3005/the_dance/process_callback?service_provider_id=11'
|
||||
@@ -68,10 +68,12 @@ var r = request.post(
|
||||
}
|
||||
})
|
||||
|
||||
console.log(getsignature(r))
|
||||
assert.equal(reqsign, getsignature(r))
|
||||
setTimeout(function () {
|
||||
console.log(getsignature(rsign))
|
||||
assert.equal(reqsign, getsignature(rsign))
|
||||
})
|
||||
|
||||
var r = request.post(
|
||||
var raccsign = request.post(
|
||||
{ url: 'https://api.twitter.com/oauth/access_token'
|
||||
, oauth:
|
||||
{ consumer_key: 'GDdmIQH6jhtmLUypg82g'
|
||||
@@ -86,10 +88,12 @@ var r = request.post(
|
||||
}
|
||||
})
|
||||
|
||||
console.log(getsignature(r))
|
||||
assert.equal(accsign, getsignature(r))
|
||||
setTimeout(function () {
|
||||
console.log(getsignature(raccsign))
|
||||
assert.equal(accsign, getsignature(raccsign))
|
||||
}, 1)
|
||||
|
||||
var r = request.post(
|
||||
var rupsign = request.post(
|
||||
{ url: 'http://api.twitter.com/1/statuses/update.json'
|
||||
, oauth:
|
||||
{ consumer_key: "GDdmIQH6jhtmLUypg82g"
|
||||
@@ -103,7 +107,11 @@ var r = request.post(
|
||||
}
|
||||
, form: {status: 'setting up my twitter 私のさえずりを設定する'}
|
||||
})
|
||||
setTimeout(function () {
|
||||
console.log(getsignature(rupsign))
|
||||
assert.equal(upsign, getsignature(rupsign))
|
||||
}, 1)
|
||||
|
||||
|
||||
|
||||
console.log(getsignature(r))
|
||||
assert.equal(upsign, getsignature(r))
|
||||
|
||||
|
||||
+50
-1
@@ -58,6 +58,20 @@ s.listen(s.port, function () {
|
||||
mydata.emit('data', 'mydata');
|
||||
mydata.emit('end');
|
||||
|
||||
// Test pipeing to a request object with a json body
|
||||
s.once('/push-json', server.createPostValidator("{\"foo\":\"bar\"}", "application/json"));
|
||||
|
||||
var mybodydata = new stream.Stream();
|
||||
mybodydata.readable = true
|
||||
|
||||
counter++
|
||||
var r2 = request.put({url:'http://localhost:3453/push-json',json:true}, function () {
|
||||
check();
|
||||
})
|
||||
mybodydata.pipe(r2)
|
||||
|
||||
mybodydata.emit('data', JSON.stringify({foo:"bar"}));
|
||||
mybodydata.emit('end');
|
||||
|
||||
// Test pipeing from a request object.
|
||||
s.once('/pull', server.createGetResponse("mypulldata"));
|
||||
@@ -99,7 +113,7 @@ s.listen(s.port, function () {
|
||||
})
|
||||
s.on('/pushjs', function (req, resp) {
|
||||
if (req.method === "PUT") {
|
||||
assert.equal(req.headers['content-type'], 'text/javascript');
|
||||
assert.equal(req.headers['content-type'], 'application/javascript');
|
||||
check();
|
||||
}
|
||||
})
|
||||
@@ -163,5 +177,40 @@ s.listen(s.port, function () {
|
||||
afterresp.pipe(v)
|
||||
v.on('end', check)
|
||||
})
|
||||
|
||||
s.on('/forward1', function (req, resp) {
|
||||
resp.writeHead(302, {location:'/forward2'})
|
||||
resp.end()
|
||||
})
|
||||
s.on('/forward2', function (req, resp) {
|
||||
resp.writeHead('200', {'content-type':'image/png'})
|
||||
resp.write('d')
|
||||
resp.end()
|
||||
})
|
||||
|
||||
counter++
|
||||
var validateForward = new ValidationStream('d')
|
||||
validateForward.on('end', check)
|
||||
request.get('http://localhost:3453/forward1').pipe(validateForward)
|
||||
|
||||
// Test pipe options
|
||||
s.once('/opts', server.createGetResponse('opts response'));
|
||||
|
||||
var optsStream = new stream.Stream();
|
||||
optsStream.writable = true
|
||||
|
||||
var optsData = '';
|
||||
optsStream.write = function (buf) {
|
||||
optsData += buf;
|
||||
if (optsData === 'opts response') {
|
||||
setTimeout(check, 10);
|
||||
}
|
||||
}
|
||||
|
||||
optsStream.end = function () {
|
||||
assert.fail('end called')
|
||||
};
|
||||
|
||||
counter++
|
||||
request({url:'http://localhost:3453/opts'}).pipe(optsStream, { end : false })
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user