Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mock out http calls with record/replay functionality
* Use talkback to record any http calls to a real server * Record them to the tapes/ directory * This means it can be tested by those who don't have a server up and running. * Can remove the hidden variables in travis * Will do real api calls when tapes don't exist and record new ones Not all tapes committed cause I was concerned about leaking steamid and ips
- Loading branch information
1 parent
b47fba7
commit 6cd0926
Showing
20 changed files
with
278 additions
and
207 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* eslint-env mocha */ | ||
/* | ||
* Shareable items between tests | ||
*/ | ||
import { URL } from 'url'; | ||
import * as querystring from 'querystring'; | ||
import * as chai from 'chai'; | ||
import * as chaiAsPromised from 'chai-as-promised'; | ||
import TalkbackServer from 'talkback/server'; | ||
import { RecordMode } from 'talkback/options'; | ||
import Tape from 'talkback/tape'; | ||
import { Req } from 'talkback/types'; | ||
import { SdtdServer } from '../lib/index'; | ||
|
||
// talkback's typescript/import returns different stuff than the require, so disable the eslint alert for now | ||
//eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const talkback = require('talkback'); | ||
|
||
class TestGlobals { | ||
testServer: SdtdServer; | ||
proxy: TalkbackServer; | ||
|
||
constructor() { | ||
this.testServer = { | ||
ip: "localhost", | ||
port: "5544", | ||
adminUser: 'fakeadminuser', | ||
adminToken: 'fakeadmintoken' | ||
}; | ||
const host = `http://${process.env.TESTIP}:${process.env.TESTPORT}`; | ||
this.proxy = talkback({ | ||
host: host, | ||
record: RecordMode.NEW, | ||
port: 5544, | ||
path: `${__dirname}/tapes/`, | ||
silent: true, | ||
ignoreQueryParams: ['adminuser', 'admintoken'], | ||
tapeNameGenerator: (tapeNumber: number, tape: Tape): string => { | ||
const url = new URL(tape.req.url, host); | ||
const query = querystring.parse(url.search.substr(1)); | ||
delete query.adminuser; | ||
delete query.admintoken; | ||
return url.pathname.replace(/\//g, '_') + '_' + querystring.stringify(query, '_', '_') + ".json5"; | ||
}, | ||
requestDecorator: function(req: Req) { | ||
const url = new URL(req.url, host); | ||
const query = querystring.parse(url.search.substr(1)); | ||
query.adminuser = process.env.TESTADMINUSER || 'unknown'; | ||
query.admintoken = process.env.TESTADMINTOKEN || 'unknown'; | ||
url.search = '?' + querystring.stringify(query); | ||
req.url = url.toString().replace(host, ''); | ||
return req; | ||
} | ||
}); | ||
} | ||
|
||
setTestServer(testServer: SdtdServer): void { | ||
this.testServer = testServer; | ||
} | ||
getTestServer(): SdtdServer { | ||
return this.testServer; | ||
} | ||
|
||
setProxy(proxy: TalkbackServer): void { | ||
this.proxy = proxy; | ||
} | ||
getProxy(): TalkbackServer { | ||
return this.proxy; | ||
} | ||
} | ||
|
||
const testGlobals = new TestGlobals(); | ||
export default testGlobals; | ||
|
||
require('dotenv').config(); | ||
|
||
chai.use(chaiAsPromised); | ||
|
||
before(function(done) { | ||
testGlobals.getProxy().start(() => done()); | ||
}); | ||
after(function() { | ||
testGlobals.getProxy().close(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,11 @@ | ||
'use strict'; | ||
import g from './_globals'; | ||
import * as chai from 'chai'; | ||
import * as chaiAsPromised from 'chai-as-promised'; | ||
import { getAllowedCommands, SdtdServer } from '../lib/index'; | ||
|
||
require('dotenv').config(); | ||
|
||
chai.use(chaiAsPromised); | ||
|
||
const testServer: SdtdServer = { | ||
ip: process.env.TESTIP as string, | ||
port: process.env.TESTPORT as string, | ||
adminUser: process.env.TESTADMINUSER as string, | ||
adminToken: process.env.TESTADMINTOKEN as string | ||
}; | ||
import { getAllowedCommands } from '../lib/index'; | ||
|
||
describe('/api/getAllowedCommands', async () => { | ||
it('Returns an array', async () => { | ||
const response = await getAllowedCommands(testServer); | ||
const response = await getAllowedCommands(g.getTestServer()); | ||
chai.expect(response.commands).to.be.a('array'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.