Skip to content

Commit

Permalink
Fix api server to new endpoints format
Browse files Browse the repository at this point in the history
  • Loading branch information
tejerka committed Jun 4, 2018
1 parent 94c5584 commit 57f7e13
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 87 deletions.
5 changes: 3 additions & 2 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class App {
}

buildEndpoints() {
this.endpoints = [];
this.endpoints = this.endpoints.filter((endpoint) => endpoint.source !== 'file');

read(mocksDirectory)
.filter(fileName => this.getEndpointFileExtension(fileName) !== undefined)
Expand All @@ -62,7 +62,8 @@ class App {

return {
...endpoint,
id: HttpMockServer.getNewEndpointId()
id: HttpMockServer.getNewEndpointId(),
source: 'file'
};
})
);
Expand Down
112 changes: 27 additions & 85 deletions app/http-api-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,104 +12,46 @@ class HttpApiServer {
this.api
.route('/api/endpoints')
.post((req, res) => {
if (
!req.body.uri ||
!req.body.status ||
!req.body.body ||
!req.body.headers
) {
res.writeHead(400);
let newEndpoint = req.body;

if (!newEndpoint.request) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.write(
JSON.stringify({
error: 'Endpoint request required',
})
);
res.end();
return;
}

let createNew = true;
let uriFounded = false;
if (!newEndpoint.response) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.write(
JSON.stringify({
error: 'Endpoint response required',
})
);
res.end();
return;
}

this.app.endpoints.forEach((endpoint, index) => {
if (req.body.uri === endpoint.uri) {
if (req.body.method === endpoint.method) {
this.app.endpoints[index] = {
uri: req.body.uri,
method: req.body.method,
status: req.body.status,
time: req.body.time,
body: req.body.body,
headers: req.body.headers,
velocity: req.body.velocity
? req.body.velocity
: { enabled: true },
};
createNew = false;
} else if (!endpoint.method && createNew) {
this.app.endpoints.splice(index, 0, {
uri: req.body.uri,
method: req.body.method,
status: req.body.status,
time: req.body.time,
body: req.body.body,
headers: req.body.headers,
velocity: req.body.velocity
? req.body.velocity
: { enabled: true },
});
createNew = false;
}
uriFounded = true;
} else if (uriFounded && createNew) {
this.app.endpoints.splice(index, 0, {
uri: req.body.uri,
method: req.body.method,
status: req.body.status,
time: req.body.time,
body: req.body.body,
headers: req.body.headers,
velocity: req.body.velocity
? req.body.velocity
: { enabled: true },
});
createNew = false;
}
});
newEndpoint = {
...newEndpoint,
id: httpMockServer.getNewEndpointId(),
source: 'user',
};

if (createNew) {
this.app.endpoints.push({
uri: req.body.uri,
method: req.body.method,
status: req.body.status,
time: req.body.time,
body: req.body.body,
headers: req.body.headers,
velocity: req.body.velocity ? req.body.velocity : { enabled: true },
});
}
this.app.endpoints.push(newEndpoint);

res.writeHead(204);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(newEndpoint));
res.end();
})
.get((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(this.app.endpoints));
res.end();
})
.delete((req, res) => {
if (!req.body.uri) {
res.writeHead(400);
res.end();
return;
}

this.app.endpoints.forEach((endpoint, index) => {
if (
req.body.uri === endpoint.uri &&
(!req.body.method || req.body.method === endpoint.method)
) {
this.app.endpoints.splice(index, 1);
}
});

res.writeHead(204);
res.end();
});

this.api.use('/', express.static('.'));
Expand Down

0 comments on commit 57f7e13

Please sign in to comment.