var assert = require('assert'),
  fs = require('fs'),
  request = require('supertest'),
  express = require('express');

var danzi = require('..');

var uploadPath = __dirname + '/upload';

var app = express();

app
  .use(express.bodyParser())
  .use(danzi({ path: uploadPath }))
  .use(function(req, res) {
    if (req.files && req.files.hasOwnProperty('file')) {
      res.send(req.files.file.uri);
    }
    else {
      res.json(req.body);
    }
  });

describe('post request test', function() {
  it('should response to request param', function(done) {
    var param = { name: "test" };
    request(app)
      .post('/')
      .send(param)
      .expect('Content-Type', /json/)
      .expect(200)
      .expect(param, done);
  });

  it('should parse x-www-form-urlencoded', function(done){
    request(app)
      .post('/')
      .set('Content-Type', 'application/x-www-form-urlencoded')
      .send('user=alice')
      .expect({user: "alice"}, done);
  });

  describe('with multipart/form-data', function() {
    it('should populate req.body', function(done) {
      var req = request(app);
      req
        .post('/')
        .field('user', 'Alice')
        .expect({user: "Alice"}, done);
    });
  });
});

describe('upload a file', function() {
  it('should has uploaded file', function(done) {
    request(app)
      .post('/')
      .attach('file', __dirname + '/fixture.txt')
      .expect(200)
      .end(function(err, res) {
        fs.exists(res.text, function(exists) {
          assert.equal(exists, true);
          done();
        });
      });
  });

  after(function(done) {
    var files = fs.readdirSync(uploadPath);
    for(var key in files) {
      fs.unlinkSync(uploadPath + '/' + files[key]);
    }
    fs.rmdirSync(uploadPath);
    done();
  });

});

@@ -183,16 +183,61 @@
 load';%0A%0A
+describe('post request test', function() %7B%0A  
 var app 
@@ -254,12 +254,16 @@
 );%0A%0A
+  
 app%0A
+  
   .u
@@ -285,24 +285,26 @@
 Parser())%0A  
+ 
+ 
 .use(danzi(%7B
@@ -328,16 +328,27 @@
  %7D))
+;%0A
 %0A  
-.use(
+app.post('/', 
 func
@@ -517,51 +517,8 @@
 );%0A%0A
-describe('post request test', function() %7B%0A
   it
@@ -1227,21 +1227,19 @@
 ;%0A  %7D);%0A
-%7D);%0A%0A
+%0A  
 describe
@@ -1269,16 +1269,18 @@
 ion() %7B%0A
+  
   it('sh
@@ -1286,17 +1286,20 @@
 hould ha
-s
+ve a
  uploade
@@ -1324,24 +1324,26 @@
 done) %7B%0A    
+  
 request(app)
@@ -1341,32 +1341,34 @@
 uest(app)%0A      
+ 
+ 
 .post('/')%0A     
@@ -1358,24 +1358,26 @@
  .post('/')%0A
+  
       .attac
@@ -1412,32 +1412,34 @@
 ure.txt')%0A      
+ 
+ 
 .expect(200)%0A   
@@ -1427,32 +1427,34 @@
    .expect(200)%0A
+  
       .end(funct
@@ -1477,16 +1477,18 @@
         
+ 
+ 
 fs.exist
@@ -1528,16 +1528,18 @@
         
+  
 assert.e
@@ -1564,24 +1564,26 @@
 ;%0A          
+ 
+ 
 done();%0A    
@@ -1582,24 +1582,26 @@
 ();%0A        
+  
 %7D);%0A      %7D)
@@ -1594,24 +1594,34 @@
   %7D);%0A      
+ 
+ %7D);%0A    
 %7D);%0A  %7D);%0A%0A 
@@ -1618,19 +1618,21 @@
 ;%0A  %7D);%0A
-%0A  
+%7D);%0A%0A
 after(fu
@@ -1642,26 +1642,24 @@
 ion(done) %7B%0A
-  
   var files 
@@ -1690,18 +1690,16 @@
 ath);%0A  
- 
- 
 for(var 
@@ -1710,26 +1710,24 @@
 in files) %7B%0A
-  
     fs.unlin
@@ -1766,22 +1766,18 @@
 ey%5D);%0A  
- 
- 
 %7D%0A
-  
   fs.rmd
@@ -1798,26 +1798,24 @@
 ath);%0A  
- 
- 
 done();%0A
   %7D);%0A%0A%7D
@@ -1806,19 +1806,12 @@
 done();%0A
-  %7D);%0A%0A
 %7D);%0A

Refactor the test