Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/marketionist/node-testing-server

Simple Node.js server to generate .html, .json, .js, .css, .jpg, .png pages for end-to-end and API testing
https://github.com/marketionist/node-testing-server

nodejs server test test-automation testing-tools

Last synced: about 1 month ago
JSON representation

Simple Node.js server to generate .html, .json, .js, .css, .jpg, .png pages for end-to-end and API testing

Awesome Lists containing this project

README

        

# node-testing-server

[![Actions Status](https://github.com/Marketionist/node-testing-server/workflows/Build%20and%20Test/badge.svg)](https://github.com/Marketionist/node-testing-server/actions)
[![npm version](https://img.shields.io/npm/v/node-testing-server.svg)](https://www.npmjs.com/package/node-testing-server)
[![NPM License](https://img.shields.io/npm/l/node-testing-server.svg)](https://github.com/Marketionist/node-testing-server/blob/master/LICENSE)

Simple node.js server to generate .html, .json, .js, .css, .jpg, .png pages for end-to-end and API testing

## Supported versions
[Node.js](http://nodejs.org/):
- 10.x
- 11.x
- 12.x
- 13.x
- 14.x
- 15.x
- 16.x
- 17.x
- 18.x
- 19.x
- 20.x

## Installation
`npm install node-testing-server --save-dev`

## Importing and configuring
You can require node-testing-server and configure it like this:

```javascript
let { nodeTestingServer } = require('node-testing-server');

// Settings for node testing server
nodeTestingServer.config = {
hostname: 'localhost',
port: 3001,
logsEnabled: 0,
pages: {
'/test.html': `


  • First

  • Second

  • Third

  • Fourth

  • Fifth

  • Sixth

  • Seventh

  • Eighth

  • Ninth

  • Tenth

`
}
};
```

Also if you want to specify a status code that will be returned by the page:

```javascript
let { nodeTestingServer } = require('node-testing-server');

// Settings for node testing server
nodeTestingServer.config = {
hostname: 'localhost',
port: 3001,
logsEnabled: 0,
pages: {
'/test.html': {
pageStatusCode: 201,
pageBody: `


  • First

  • Second

  • Third

  • Fourth

  • Fifth

  • Sixth

  • Seventh

  • Eighth

  • Ninth

  • Tenth

`
}
}
};
```

By default logs are disabled (`logsEnabled` is set to 0). You can set
`logsEnabled` config to one of 3 levels:
- 0 - logs disabled
- 1 - partial logs are enabled - prints out:
* incoming request METHOD, URL and outcoming response CODE
* pages that were generated or served
- 2 - full logs are enabled - prints out:
* incoming request METHOD, URL and outcoming response CODE
* pages that were generated or served
* incoming request headers
* response time

## Usage
Start and stop server like this:

```javascript
// Start node testing server
nodeTestingServer.start();

// Stop node testing server
nodeTestingServer.stop();
```

If you will configure the `/test.html` server page as described in
[Importing and configuring](#importing-and-configuring) section above, and send
a `GET` request to `http://localhost:3001/test.html`, then the server will
return html page with:

```html


  • First

  • Second

  • Third

  • Fourth

  • Fifth

  • Sixth

  • Seventh

  • Eighth

  • Ninth

  • Tenth


```

There are also 2 default pre-configured pages that you can hit:

1. Sending `GET` request to `http://localhost:3001/` will return:

```html


Node testing server - main page

Main page of node testing server


This page was constructed for testing purposes.


```

> Note: if you want to serve your own page from `http://localhost:3001/` - just
> create it in `public/index.html` in your root folder - otherwise it will be
> served from `node_modules/node-testing-server/public/index.html`

2. Sending `POST` request to `http://localhost:3001/post` with `{"test1":1,"test2":"Test text"}`
in the body will return a string with request headers and body:

```
`Incoming request headers: {"content-type":"application/json","connection":"close","content-length":"31","host":"localhost:3001"}
Incoming request body: {"test1":1,"test2":"Test text"}`
```

You can see live examples of node-testing-server usage in
[page-content-tests.js](https://github.com/Marketionist/node-testing-server/blob/master/tests/page-content-tests.js),
in [Protractor tests](https://github.com/Marketionist/protractor-numerator/blob/master/test/spec.ts)
and in [TestCafe tests](https://github.com/Marketionist/testcafe-cucumber-steps/blob/master/tests/testing-server.js)

## Thanks
If this package was helpful to you, please give it a **★ Star**
on [GitHub](https://github.com/Marketionist/node-testing-server)