shellphon / quick-local-service

Cli for start web static page
0 stars 0 forks source link

Test proxy post request #4

Closed shellphon closed 7 years ago

shellphon commented 7 years ago

supertest post to test the post proxy, always get 404 instead of 200. While use example directory handly, post proxy is fine.

shellphon commented 7 years ago
var QLS = require('../index.js');

var expect = require('chai').expect,
    qls = QLS();

var superagent = require('supertest');

var path = require('path');

var autoPort = {
    port:10098,
    getPort:function(){
        return this.port++;
    }
};

describe("quick local service 【for post test】", function() {

    it("QLS().run() with proxy should proxy the post request", function(done) {
        var proxyPort = autoPort.getPort(),
            serverPort = autoPort.getPort();
        var app = require('koa')();
        var router = require('koa-router')();
        router.post('/api/hello', function *(next){
            this.body = {"res":1};
        });

        router.get('/api/test',function *(next){
            this.body = {"result":1};
        });
        app.use(router.routes());

        var interface = new Promise(function(resolve,reject){
            app.listen(proxyPort, function(){
                console.log('proxy server start:'+proxyPort);
                resolve();
            });
        });

        interface.then(function(){
            qls.run({
                port: serverPort,
                dir: path.resolve(__dirname, '../example/html'),
                proxy:{
                    '/api':{
                        host:'http://127.0.0.1:' + proxyPort,
                        pathRewrite:{
                            '^/api':'/api'
                        }
                    }
                },
                cbk: function(){

                    superagent('http://127.0.0.1:' + serverPort)
                        .post('/api/hello')
                        //.get('/api/test')
                        .expect(200, done);
                }
            });
        }).catch(function(){
            done();
        });
    });
});
shellphon commented 7 years ago

I found that if the post test run first, that won't get error.