All files / test/interface-proxy request_test.js

100% Statements 60/60
100% Branches 4/4
100% Functions 28/28
100% Lines 58/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123  1x       1x 1x 1x 1x 1x       1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x       1x 1x 1x 1x       1x 1x 1x 1x       1x 1x 1x 1x     1x 1x         1x 1x       1x 1x 1x 1x 1x         1x   1x 1x           4x 1x       1x 1x         3x 1x       1x 1x         1x 1x     1x 1x         1x         1x 1x 1x 1x      
// import {InterfaceProxy} from '../../src/index.js';
import iProxy from './iProxy';
 
describe("instance interface requests", () => {
    // params 参数自动匹配
    it("should extract params according to config.param", (done) => {
        iProxy.user('999', 'wdzxc').then(data => {
            expect(data.query.id).toBe('999');
            expect(data.query.name).toBe('wdzxc');
            done();
        });
    });
    // params 参数默认值
    it("with params config should regard as default value to query params", (done) => {
        iProxy.user().then(data => {
            expect(data.query.race).toBe('human');
            expect(data.query.job).toBe('jobless');
            done();
        });
    });
    // params 参数默认值覆盖
    it("with params config should be cover by request query params", (done) => {
        iProxy.user({id: '999', job: 'postman'}).then(data => {
            expect(data.query.id).toBe('999');
            expect(data.query.race).toBe('human');
            expect(data.query.job).toBe('postman');
            done();
        });
    });
    // 请求 dataType为 html
    it("should support dataType as html", (done) => {
        iProxy.multiResponseHtml().then(data => {
            expect(data).toBe("<p>hey</p>");
            done();
        });
    });
    // 请求 dataType为 text
    it("should support dataType as text", (done) => {
        iProxy.multiResponseText().then(data => {
            expect(data).toBe('hey');
            done();
        });
    });
    // 配置项 origin
    it("should replace request origin according to origin config", (done) => {
        iProxy.userOrigin().then(data => {
            expect(data.hostname).toEqual('127.0.0.1');
            done();
        });
    });
    it("should support config pathParams", (done) => {
        iProxy.pathParams.get(null, {
            pathParams: {
                id: 888,
            }
        }).then(data => {
            expect(data.baseUrl).toBe('/api/pathParams/888');
            done();
        })
    });
    // 请求预处理 - 当请求方法为post时,将query参数自动转移到body
    it("should move query params to body when request method is post", (done) => {
        iProxy.postTest({data: 666}).then((data, response) => {
            expect(data).toEqual({data: 666});
            expect(response).toEqual({success: true, data: {data: 666}});
            done();
        });
    });
});
 
describe("instance method's api", () => {
    // 支持 post,delete,head,option api
    it("should have request method helper [get,delete,head,option]", (done) => {
        Promise.all([
            iProxy.user.get(),
            iProxy.user.delete(),
            iProxy.user.head(),
            iProxy.user.options(),
        ]).then(data => {
            expect(data.map(res => res && res.method)).toEqual(['GET', 'DELETE', null, null]);
            done();
        });
    });
    // 支持 post,put,patch methods
    it("should have request method helper [post,put,patch]", (done) => {
        Promise.all([
            iProxy.user.post(),
            iProxy.user.put(),
            iProxy.user.patch(''),
        ]).then(data => {
            expect(data.map(res => res && res.method)).toEqual(['POST', 'PUT', 'PATCH']);
            done();
        });
    });
    // 支持 post 使用调用函数的参数传递数据
    it("should post arg data as text/plain", (done) => {
        iProxy.jsonRequest.post('wdzxc', {
            headers: {
                "Content-Type": "text/plain",
            }
        }).then(data => {
            expect(data.body).toEqual("wdzxc");
            done();
        });
    });
    it("should support multiple methods config", (done) => {
        let user = {
            name: 'lina',
            job: 'sorceress',
            race: 'human',
        };
        iProxy.user.post(user, {
            headers: {
                'x-token': '777',
            },
        }).then(data => {
            expect(data.method).toBe('POST');
            expect(data.body).toEqual(user);
            expect(data.headers['x-token']).toBe('777');
            done();
        });
    });
});