rest_server.test.ts 5.07 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
3
4
5
6
7
8
9
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import assert from 'assert';
import fs from 'fs';
import path from 'path';

import fetch from 'node-fetch';

10
import globals, { resetGlobals } from 'common/globals/unittest';
liuzhe-lz's avatar
liuzhe-lz committed
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
import { RestServer, UnitTestHelpers } from 'rest_server';
import * as mock_netron_server from './mock_netron_server';

let restServer: RestServer;
let endPoint: string;
let endPointWithoutPrefix: string;
let netronHost: string;

const logFileContent = fs.readFileSync(path.join(__dirname, 'log/mock.log'));
const webuiIndexContent = fs.readFileSync(path.join(__dirname, 'static/index.html'));
const webuiScriptContent = fs.readFileSync(path.join(__dirname, 'static/script.js'));

/* Test cases */

// Test cases will be run twice, the first time without URL prefix, and the second time with a URL prefix.

// NNI manager APIs are covered by old tests.
// In future RestServer should not be responsible for API implementation.

// Download a log file.
async function testLogs(): Promise<void> {
    const res = await fetch(urlJoin(endPoint, '/logs/mock.log'));
    const contentType = res.headers.get('Content-Type')!;
    assert.ok(res.ok);
    assert.ok(contentType.startsWith('text/plain'));  // content type can influence browser behavior
    assert.equal(await res.text(), logFileContent);
}

// Proxy a GET request to Netron.
async function testNetronGet(): Promise<void> {
    const res = await fetch(urlJoin(endPoint, '/netron/mock/get-path'));
    const req = await res.json();  // the mock server send request info back as response
    assert.ok(res.ok);
    assert.equal(req.headers.host, netronHost);
    assert.equal(req.url, '/mock/get-path');
}

// Proxy a POST request to Netron.
async function testNetronPost(): Promise<void> {
    const postData = 'hello netron';
    const res = await fetch(urlJoin(endPoint, '/netron/post-path'), { method: 'POST', body: postData });
    const req = await res.json();
    assert.ok(res.ok);
    assert.equal(req.url, '/post-path');
    assert.equal(req.body, postData);
}

// Access web UI index page.
async function testWebuiIndex(): Promise<void> {
    const res = await fetch(endPoint);
    assert.ok(res.ok);
    assert.equal(await res.text(), webuiIndexContent);
}

// Access web UI resource file (js, css, image, etc).
async function testWebuiResource(): Promise<void> {
    const res = await fetch(urlJoin(endPoint, '/script.js'));
    assert.ok(res.ok);
    assert.equal(await res.text(), webuiScriptContent);
}

// Access web UI routing path ("/oview", "/detail", etc).
// This should also send index page.
async function testWebuiRouting(): Promise<void> {
    const res = await fetch(urlJoin(endPoint, '/not-exist'));
    assert.ok(res.ok);
    assert.equal(await res.text(), webuiIndexContent);
}

// When URL prefix is set, send a request without that prefix.
async function testOutsidePrefix(): Promise<void> {
    const res = await fetch(endPointWithoutPrefix);
    assert.equal(res.status, 404);

    const res2 = await fetch(urlJoin(endPointWithoutPrefix, '/not-exist'));
    assert.equal(res2.status, 404);
}

/* Register test cases */

describe('## rest_server ##', () => {
    it('logs', () => testLogs());
    it('netron get', () => testNetronGet());
    it('netron post', () => testNetronPost());
    it('webui index', () => testWebuiIndex());
    it('webui resource', () => testWebuiResource());
    it('webui routing', () => testWebuiRouting());

    // I don't know how to add "between test cases hook".
    // This is a workaround to reset REST server with URL prefix.
    it('// re-configure rest server', () => configRestServer('url/prefix'));

    it('prefix logs', () => testLogs());
    it('prefix netron get', () => testNetronGet());
    it('prefix netron post', () => testNetronPost());
    it('prefix webui index', () => testWebuiIndex());
    it('prefix webui resource', () => testWebuiResource());
    it('prefix webui routing', () => testWebuiRouting());
    it('outside prefix', () => testOutsidePrefix());
});

/* Configure test environment */

before(async () => {
    await configRestServer();

    const netronPort = await mock_netron_server.start();
    netronHost = `localhost:${netronPort}`;
    UnitTestHelpers.setNetronUrl('http://' + netronHost);
});

after(async () => {
    await restServer.shutdown();
124
    resetGlobals();
liuzhe-lz's avatar
liuzhe-lz committed
125
126
127
128
129
130
131
});

async function configRestServer(urlPrefix?: string) {
    if (restServer !== undefined) {
        await restServer.shutdown();
    }

132
    globals.paths.logDirectory = path.join(__dirname, 'log');
liuzhe-lz's avatar
liuzhe-lz committed
133
134
    UnitTestHelpers.setWebuiPath(path.join(__dirname, 'static'));

liuzhe-lz's avatar
liuzhe-lz committed
135
    restServer = new RestServer(0, urlPrefix ?? '');
liuzhe-lz's avatar
liuzhe-lz committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
    await restServer.start();
    const port = UnitTestHelpers.getPort(restServer);

    endPointWithoutPrefix = `http://localhost:${port}`;
    endPoint = urlJoin(endPointWithoutPrefix, urlPrefix ?? '');
}

function urlJoin(part1: string, part2: string): string {
    if (part1.endsWith('/')) {
        part1 = part1.slice(0, -1);
    }
    if (part2.startsWith('/')) {
        part2 = part2.slice(1);
    }
    if (part2 === '') {
        return part1;
    }
    return part1 + '/' + part2;
}