Permalink
Browse files

test cookie jars

  • Loading branch information...
1 parent b502452 commit 977e949c8cd18670cedb6a87f5e406fd6e922de7 @contra contra committed Dec 4, 2012
Showing with 29 additions and 7 deletions.
  1. +5 −4 lib/main.coffee
  2. +2 −1 package.json
  3. +22 −2 test/main.coffee
View
9 lib/main.coffee
@@ -1,11 +1,10 @@
request = require 'request'
async = require 'async'
qs = require 'qs'
+silo = require 'silo'
ignoredHeaders = ["cookie","referer","host","connection","accept-encoding"]
-defaultCookies =
- get: (id, cb) -> cb()
- set: (id, cookies, cb) -> cb()
+defaultCookies = new silo.Memory()
module.exports =
createProxy: (opt={}) ->
@@ -24,11 +23,13 @@ module.exports =
opt.cookies.get id, (err, cookie) ->
return res.send 500, err if err?
jar = request.jar()
- jar.add cookie if cookie?
+ jar.add request.cookie cookie if cookie?
head = {}
head[k]=v for k,v of req.headers when not (k.toLowerCase() in ignoredHeaders)
+ delete req.body if typeof req.body is 'object'
+
ropt =
headers: head
method: req.method
View
3 package.json
@@ -11,7 +11,8 @@
"coffee-script":"*",
"async": "*",
"request": "*",
- "qs": "*"
+ "qs": "*",
+ "silo": "*"
},
"devDependencies":{
"mocha":"*",
View
24 test/main.coffee
@@ -17,19 +17,29 @@ describe 'moxy', ->
proxy = moxy.createProxy()
app = express()
+ app.use express.bodyParser()
app.all '/proxy/:id', proxy
app.listen 8008
+ second = false
app2 = express()
+ app2.use express.cookieParser()
+ app2.use express.bodyParser()
app2.get '/test', (req, res) ->
should.exist req.header('Client-Header')
req.header('Client-Header').should.equal 'wot'
should.exist req.query.wat
req.query.wat.should.equal 'dood'
+ if second
+ should.exist req.cookies.wat
+ req.cookies.wat.should.equal 'dood'
+
res
.status(200)
+ .cookie('wat','dood')
.set('Custom-Header', 'test')
- .send 'hello world'
+ .send('hello world')
+ second = true
app2.listen 8009
@@ -45,7 +55,17 @@ describe 'moxy', ->
res.statusCode.should.equal 200
res.headers['custom-header'].should.equal 'test'
body.should.equal 'hello world'
- done()
+
+ # test cookie jar
+ request "http://localhost:8008/proxy/test/?surl=http://localhost:8009/test?wat=dood", opt, (err, res, body) ->
+ should.not.exist err, 'res error'
+ should.exist res, 'res'
+ should.exist body, 'body'
+ should.exist res.headers['custom-header']
+ res.statusCode.should.equal 200
+ res.headers['custom-header'].should.equal 'test'
+ body.should.equal 'hello world'
+ done()
describe 'use()', ->
it 'should work', (done) ->

0 comments on commit 977e949

Please sign in to comment.