Skip to content

Commit

Permalink
Updated logic to use request.query & request.url (#20)
Browse files Browse the repository at this point in the history
* Updated logic to use request.query & request.url instead of weird logic. This will allow us to modify query parameters in the middlewares easier.
* Updated dependencies, and code that has changed due to those dependencies having major changes.
* Updated docker image with node 16/newest alpine.
* Added newer versions of node to the tests. Dropping support for Node 11, 12 & 13.
* Adding step to publish to docker as now the automated builds are premium feature.
  • Loading branch information
gguridi authored Nov 19, 2022
1 parent 67cb58c commit eb2b1a2
Show file tree
Hide file tree
Showing 15 changed files with 2,459 additions and 4,011 deletions.
107 changes: 65 additions & 42 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,48 +1,71 @@
name: Publish

on:
release:
types:
- created
release:
types:
- created

jobs:
mockserver-test:
name: Unit Tests
runs-on: ubuntu-latest
strategy:
matrix:
nodejs: [11, 12, 13, 14]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.nodejs }}
- name: install dependencies
run: yarn install
- name: build and run mockserver docker
run: yarn run docker-build && yarn run docker-run
- name: run tests
run: yarn test
mockserver-test:
name: Unit Tests
runs-on: ubuntu-latest
strategy:
matrix:
nodejs: [14, 16, 18, 19]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.nodejs }}
- name: install dependencies
run: yarn install
- name: build and run mockserver docker
run: yarn run docker-build && yarn run docker-run
- name: run tests
run: yarn test

publish:
needs:
- mockserver-test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: set version envvar
run: echo ::set-env name=RELEASE_VERSION::$(echo ${GITHUB_REF:11})
- name: set release version on package.json
run: sed -i.bak 's/0\.0\.0/'$RELEASE_VERSION'/g' package.json
- name: check package
run: cat package.json
- uses: actions/setup-node@v1
with:
node-version: 13
- name: install dependencies
run: yarn install
- name: run build
run: yarn build
- uses: JS-DevTools/npm-publish@v1
with:
token: ${{ secrets.NPM_TOKEN }}
publish-to-npm:
needs:
- mockserver-test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: set version envvar
run: echo ::set-env name=RELEASE_VERSION::$(echo ${GITHUB_REF:11})
- name: set release version on package.json
run: sed -i.bak 's/0\.0\.0/'$RELEASE_VERSION'/g' package.json
- name: check package
run: cat package.json
- uses: actions/setup-node@v1
with:
node-version: 16
- name: install dependencies
run: yarn install
- name: run build
run: yarn build
- uses: JS-DevTools/npm-publish@v1
with:
token: ${{ secrets.NPM_TOKEN }}

publish-to-docker:
needs:
- mockserver-test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: set version envvar
run: echo ::set-env name=RELEASE_VERSION::$(echo ${GITHUB_REF:11})
- uses: actions/setup-node@v3
with:
node-version: 16
- name: install dependencies
run: yarn install
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: build image
run: docker build -t gguridi/mockserver:version-$RELEASE_VERSION .
- name: push image
run: docker push -t gguridi/mockserver:version-$RELEASE_VERSION .
34 changes: 17 additions & 17 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ name: Testing
on: push

jobs:
mockserver-test:
name: Unit Tests
runs-on: ubuntu-latest
strategy:
matrix:
nodejs: [11, 12, 13, 14]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.nodejs }}
- name: install dependencies
run: yarn install
- name: build and run mockserver docker
run: yarn run docker-build && yarn run docker-run
- name: run tests
run: yarn test
mockserver-test:
name: Unit Tests
runs-on: ubuntu-latest
strategy:
matrix:
nodejs: [14, 16, 18, 19]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.nodejs }}
- name: install dependencies
run: yarn install
- name: build and run mockserver docker
run: yarn run docker-build && yarn run docker-run
- name: run tests
run: yarn test
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"printWidth": 120,
"tabWidth": 4,
"trailingComma": "all"
}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:12.19.0-alpine3.12
FROM node:16-alpine3.16

ENV SERVER_FOLDER=/opt/mockserver

Expand Down
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@
"author": "Gorka Guridi <[email protected]>",
"license": "MIT",
"dependencies": {
"body-parser": "^1.19.0",
"commander": "^6.1.0",
"express": "^4.17.1",
"body-parser": "^1.20.1",
"commander": "^9.4.1",
"express": "^4.18.2",
"header-case-normalizer": "^1.0.3",
"simple-combinatorics": "^1.0.2",
"winston": "^3.3.3"
"winston": "^3.8.2"
},
"devDependencies": {
"@babel/cli": "^7.11.6",
"@babel/core": "^7.11.6",
"@babel/preset-env": "^7.11.5",
"babel-loader": "^8.1.0",
"@babel/cli": "^7.19.3",
"@babel/core": "^7.19.3",
"@babel/preset-env": "^7.19.3",
"babel-loader": "^9.1.0",
"frisby": "^2.1.3",
"jest": "^26.4.2",
"node-mocks-http": "^1.9.0",
"supertest": "^5.0.0"
"jest": "^29.3.1",
"node-mocks-http": "^1.12.1",
"supertest": "^6.3.1"
},
"jest": {
"setupFiles": [
Expand Down
16 changes: 6 additions & 10 deletions src/paths.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import winston from "winston";
import path from "path";
import fs from "fs";
import { getPath, getQuery, getHeadersWatched } from "./request";
import path from "path";
import combinatorics from "simple-combinatorics";
import winston from "winston";
import { getHeadersWatched, getPath, getQuery } from "./request";

export const noEmpty = (item) => {
return item;
Expand Down Expand Up @@ -38,7 +38,7 @@ export const getFiles = (request, options) => {

const getQueryElements = (request) => {
const query = getQuery(request);
return query ? [`--${query}`] : [];
return Object.entries(query).map(([key, value]) => (value ? `--${key}=${value}` : `--${key}`));
};

const getBodyElements = (request) => {
Expand All @@ -59,13 +59,9 @@ export const getFilenames = (request, options) => {
);
let combinations = [[""]];
for (let i = 1; i <= elements.length; i++) {
combinations = combinations.concat(
combinatorics.permuteList(elements, i, true),
);
combinations = combinations.concat(combinatorics.permuteList(elements, i, true));
}
return combinations
.map((file) => request.method + file.join(""))
.filter(unique);
return combinations.map((file) => request.method + file.join("")).filter(unique);
};

export const getPathWildcards = (request) => {
Expand Down
9 changes: 2 additions & 7 deletions src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,11 @@ const findHeader = (headers, header) => {
};

export const getQuery = (request) => {
const url = request.url;
const index = url.indexOf("?");
return index >= 0
? decodeURIComponent(url.substring(index + 1).replace(/\?/g, ""))
: "";
return request.query;
};

export const getPath = (request) => {
const query = getQuery(request);
return decodeURIComponent(request.url).replace(`?${query}`, "");
return request.path;
};

export const getMethod = (request) => {
Expand Down
34 changes: 13 additions & 21 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import bodyParser from "body-parser";
import express from "express";
import path from "path";
import winston, { add, ExceptionHandler } from "winston";
import newProgram, {
PARSER_JSON,
PARSER_TEXT,
PARSER_URLENCODED,
PARSER_RAW,
} from "./cli";
import winston from "winston";
import newProgram, { PARSER_JSON, PARSER_RAW, PARSER_TEXT, PARSER_URLENCODED } from "./cli";
import { getContent } from "./paths";
import Response from "./response";
import express from "express";
import bodyParser from "body-parser";
export { evaluateFile, evaluateJSON } from "./evaluations";

export const logging = (level) => {
Expand Down Expand Up @@ -66,9 +61,7 @@ const delay = (res) => {
};

const handler = (program) => (req, res, next) => {
winston.debug(
`Received request ${req.url} with body ${JSON.stringify(req.body)}`,
);
winston.debug(`Received request ${req.url} with body ${JSON.stringify(req.body)}`);

try {
const [content, filepath] = getContent(req, program);
Expand All @@ -94,25 +87,24 @@ const handler = (program) => (req, res, next) => {
export const start = (args) => {
let program = newProgram();
program.parse(args);
const options = program.opts();

logging(program.logLevel);
logging(options.logLevel);

const app = express();
app.use(parser(program.body));
app.use(...middlewares("before.js", program));
app.all("*", handler(program));
app.use(...middlewares("after.js", program));
app.use(parser(options.body));
app.use(...middlewares("before.js", options));
app.all("*", handler(options));
app.use(...middlewares("after.js", options));
app.use((_, res, next) => {
delay(res);
res.send(res.body);
});

const mockserver = app.listen(program.port, () => {
const mockserver = app.listen(options.port, () => {
const host = mockserver.address().address;
const port = mockserver.address().port;
winston.info(
`Mockserver serving ${program.mocks} at http://${host}:${port}`,
);
winston.info(`Mockserver serving ${options.mocks} at http://${host}:${port}`);
});
return mockserver;
};
Loading

0 comments on commit eb2b1a2

Please sign in to comment.