Giter Site home page Giter Site logo

mockoon / commons-server Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 11.0 3.14 MB

Mockoon's commons library. Used in Mockoon desktop application and CLI.

Home Page: https://mockoon.com

License: MIT License

JavaScript 2.03% TypeScript 97.97%
electron nodejs api library rest server rest-api mock-server swagger desktop mocking prototyping mocking-utility open-api mock-data openapi3 mock-api mocking-server mockoon

commons-server's Introduction

📦This repository has been archived. You will find Mockoon's new monorepo here.




@Mockoon/commons-server

Mockoon's Commons server library regroups Mockoon's shared code used in the main application and the CLI. It contains mostly "server side" code designed to be used in a Node.js environments.


Support/feedback

You can discuss all things related to Mockoon, and ask for help, on the main application community or the CLI community. It's also a good place to discuss bugs and feature requests before opening an issue on this repository. For more chat-like discussions, you can also join our Discord server.

Contributing

If you are interested in contributing to Mockoon, please take a look at the main contributing guidelines and the ones specific to this repository.

Please also take a look at our Code of Conduct.

Documentation

You will find Mockoon's documentation on the official website. It covers Mockoon's most complex features. Feel free to contribute or ask for new topics to be covered.

Roadmap

If you want to know what will be coming in the next release you can check the global Roadmap.

New releases will be announced on Mockoon's Twitter account @GetMockoon and through the newsletter to which you can subscribe here.

commons-server's People

Contributors

barefootdeveloper avatar davidvieiratrustly avatar fabhoarau avatar gamedevtutorials avatar hugo-carbiener avatar louiealmeda avatar rubaiate avatar saucistophe avatar timon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

commons-server's Issues

Rules' AND operator with empty rules always intercept the request

Describe the bug
Having empty rules with "AND" operator serves the corresponding response even if another response's rules match

To Reproduce

  • create 2 responses
  • set the first response's rules to AND, and remove all rules
  • add one rule on the second one
  • make a call that matches the second response, you get the first one

bug#6.zip

Expected behavior
We should get the second one

Mockoon version:
all

OS / OS version:
all

Not all Mocha Tests are executed

Describe the bug
When you start the npm task test, it will only run the tests inside the spec files which are inside a subfolder of test (like test/templating-helpers/helpers.spec.ts but not the spec files which are directly inside the test folder

To Reproduce
npm run test and check the executed tests

Expected behavior (solution)
All tests should be executed. An easy fix would be to create subfolders for the utils.spec.ts and the response-rules-interpreter.spec.ts

Mockoon version:
Server Version 2.10.0

OS / OS version:
Tested on Mac OS

Review how we check header name validity

Is your feature request related to a problem? Please describe.
We currently use a home made function to check if the header name does not contain any prohibited character. TestHeaderValidity method https://github.com/mockoon/commons/blob/main/src/libs/utils.ts#L58 used here: https://github.com/mockoon/commons-server/blob/main/src/libs/server.ts#L695

Describe the solution you'd like
It may be more correct to rely on https://nodejs.org/dist/latest-v16.x/docs/api/http.html#httpvalidateheadernamename or silently fail.

`Body`, `queryParam` helpers doesn't work with `each`

Is your feature request related to a problem? Please describe.
Body, queryParam helpers doesn't work with each. It is impossible to iterate on a body's or query string's array due to the stringification. It requires the use of another iterator like repeat + @index.

Describe the solution you'd like
Stop stringifying arrays and objects by default and use the existing stringify option when stringification is wanted. This would allow objects and arrays to be reused in combination with each and potentially new helpers like length, etc.

Describe alternatives you've considered
None seems available

Additional context
This is a small breaking change for the templating system. Some investigation/testing is needed to evaluate how much breaking changes we are talking about.

queryParam helper not working inside dateTimeShift helper

Describe the bug
when using the dateTimeShift template, the queryParam helper does not work inside it

To Reproduce
create GET route with endpoint '/api/setExpiry' and response body:

{
  "ExpiryDate": "{{dateTimeShift date=(now 'yyyy-MM-dd') format='yyyy-MM-dd' months=(queryParam 'NoOfMonths')}}T11:59:15.043Z",
}

then run query: 'localhost:3000/api/setExpiry?NoOfMonths=24'

The response is the current date with no time shift.

Expected behavior
The response should be time shifted by 24 months.

something like "test": "{{#repeat (queryParam 'NoOfMonths') comma=true}}test{{/repeat}}" works as expected

Mockoon version:
v1.17.0

OS / OS version:
Windows 11

Improve performances by running servers in workers

Is your feature request related to a problem? Please describe.
Using intensive/long helpers ({{faker 'random.alphaNumeric' 999999999}}) block the main AND renderer processes. It prevents other calls to complete during the compute time.

Describe the solution you'd like
Template parsing should be done in a separated worker thread (see for example: https://blog.logrocket.com/node-js-multithreading-what-are-worker-threads-and-why-do-they-matter-48ab102f8b10/)

Binary content are broken when proxied

Describe the bug
When proxying a call containing a file (multipart/form-data) the proxied payload is wrongly converted to UTF-8 string content.

To Reproduce
Proxy to a server able to receive file upload, and call it through Mockoon's proxy with multipart/form-data content type and a file. Uploaded file should be broken.

Expected behavior
Uploaded file should be intact.

Mockoon version:
all

OS / OS version:
all

Make `split` helper compatible with SafeStrings

Describe the bug
When using split in combination with a SafeString (e.g. from queryParam), it will return an empty string.

To Reproduce
Call with /route?param1=123,456,789
{{#each (split (queryParam 'param1') ',')}} item{{this}}, {{/each}}

Expected behavior
We should get item123, item456, item789,

Mockoon version:
All

OS / OS version:
All

Helper "switch/case" doesn't work with number variables

Describe the bug
Helper switch doesn't work with variable @index from helper repeat. It returns error:

Error while serving the content: (lookupProperty(...) || (data && lookupProperty(...)) || alias2).call is not a function

To Reproduce

[
  {{#repeat 4}}
    { 
      "id": {{@index}}, 
      "name": 
        {{#switch (@index)}}
          {{#case 0}}"John"{{/case}}
          {{#case 1}}"Jack"{{/case}}
          {{#default}}"Peter"{{/default}}
        {{/switch}}
    }
  {{/repeat}}
]

Expected behavior
It should return follow response:

[
  {
    "id": 0,
    "name": "John"
  },
  {
    "id": 1,
    "name": "Jack"
  },
  {
    "id": 2,
    "name": "Peter"
  },
  {
    "id": 3,
    "name": "Peter"
  }
]

Mockoon version:
1.16.0

OS / OS version:
Windows 11

Add useful Handlebars Helper (join, len, gt, lt, gte, lte, eq)

Sometimes you need more control of your response data following Handlebar Helpers are an good addition for this:

  • join: Just an array join (params: arraydata, delimiter). Joins Array to a string with delimiter. Default Delimiter is ","
  • len: Returns the length of an array (params: arraydata). If the given data was no array 0 will be returned
  • gt: Returns boolean if a number is greater than another number (params: number1, number2) Returns false if one of the params is not a number)
  • lt: Returns boolean of a number is lower than another number (params: number1, number2) Returns false if one of the params is not a number
  • eq: Returns if number1 is same as number 2. (params: number1, number2). Returns false if one of the params is not a number)
  • lte: combines "lt or eq"
  • gte: combines "gt or eq"

This can be achieved by adding simple Handlebars Functions

Using the `header` template helper without header name returns an error

Describe the bug
Using the header template helper without header name returns an error "name argument is required to req.get".

To Reproduce
Use the helper like this: {{header}} or {{header ''}}

Expected behavior
the helper should fail silently and return an empty string if the name parameter is not present.

Helpers returning SafeString like `queryParam` doesn't work with `dateTimeShift` helper

Describe the bug
Helpers returning SafeString like queryParam doesn't work with dateTimeShift helper and returns the following error:
"Error while serving the content: dateToShift.getDate is not a function"

To Reproduce
Try to serve {{dateTimeShift date=(queryParam 'date') days=1}}

Expected behavior
Helper should convert the SafeString to a string to create a date.

Mockoon version:
all

OS / OS version:
all

Fetching body's root array inside a loop doesn't work if only using @index

Describe the bug
Fetching body's root array inside a loop doesn't work well with numbers as index 0 returns the full body.

To Reproduce
Call an endpoint with:

[
    {
        "id": 1,
        "name": "John"
    },
    {
        "id": 2,
        "name": "Doe"
    }
]

Body:

{{#repeat 2}}
{{body @index}}
{{/repeat}}

Receive this:

[
	{
		"id": 1,
		"name": "John"
	},
	{
		"id": 2,
		"name": "Doe"
	}
],
{
	"id": 2,
	"name": "Doe"
}

Expected behavior
It's working with concat, which is the expected behavior:

{{#repeat 2 }}
{{body (concat '' @index)}}
{{/repeat}}

Fix Faker.js images URL returning a 403 / update Faker.js to latest version

Describe the bug
All Faker.js images mock hosted on S3 returns a 403.
This is due to Faker.js maintainer's decision to stop working on the lib for free. S3 access was probably cut.
https://github.com/Marak/faker.js/issues/1054
Multiple solution: recreate these helpers in Mockoon, fork Faker.js and update the code, propose help to Faker.js maintainer.

To Reproduce
Use a faker helper like image.avatar

Expected behavior
Images should be available.

Mockoon version:
'>= 1.9.0

OS / OS version:
All

Add `round` helper

Is your feature request related to a problem? Please describe.
We have a floor and a ceil helpers, but no round helper.

Describe the solution you'd like
Add a round helper using Math.round().

Add baseUrl variable to templating RequestHelpers

Is your feature request related to a problem? Please describe.

I wanted to return an absolute path on a response, ideally it starts with the baseUrl where my current mockAPI is served on. The sample request response looks like this:

GET: http://localhost:3001/api/users/123

{
    "id": "123"
    "name": "Foobar",
    "imageUrl": "http://localhost:3001/api/images/123"
}

Currently, there's no way to achieve this.

Describe the solution you'd like

Adding a baseUrl will enable us to do this:

GET: /users/:userId

{
    "id": "{{urlParam 'userId'}}"
    "name": "Foobar",
    "imageUrl": "{{baseUrl}}/images/{{urlParam 'userId'}}"
}

Describe alternatives you've considered
Tried using hostname etc..

Additional context
Discussion here: mockoon/mockoon#466

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.