Skip to content

Commit

Permalink
Add HTTP Request mock contract
Browse files Browse the repository at this point in the history
  • Loading branch information
martindale committed May 9, 2022
1 parent 8b82ff1 commit f55d120
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
31 changes: 31 additions & 0 deletions contracts/http_request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as Remote from '../types/remote';

/**
* Model of the HTTP Request I/O monad.
* @param {Object} input Expected to be a map of configuration values.
* @returns {Promise} Promise which resolves on completion.
*/
export default function HTTP_REQUEST (input = {}) {
const MAX_TIME_MS = 250;
const request = this;
const promise = new Promise((resolve, reject) => {
const timer = setTimeout(() => {
reject('Timeout!');
}, MAX_TIME_MS);

const remote = new Remote({
authority: `${request.host}:${request.port}`
});

const options = await remote._OPTIONS('/');
const result = await remote._GET(request.path);

clearTimeout(timer);

resolve({
request, options, result
});
});

return promise;
}
41 changes: 31 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"homepage": "https://github.com/FabricLabs/web#readme",
"dependencies": {
"@fabric/core": "FabricLabs/fabric#portal-staging",
"@fabric/core": "FabricLabs/fabric#security/dependencies",
"body-parser": "^1.18.3",
"canvas": "^2.5.0",
"d3": "^5.9.2",
Expand Down
4 changes: 4 additions & 0 deletions types/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ class Remote extends Actor {
'Content-Type': CONTENT_TYPE
};

if (params.headers) {
headers = Object.assign({}, headers, params.headers);
}

let opts = {
method: type,
headers: headers
Expand Down

0 comments on commit f55d120

Please sign in to comment.