Giter Club home page Giter Club logo

stub-server's People

Contributors

gko avatar tkrotoff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

stub-server's Issues

Server crash when request only headers on well configured route

Context

We have correctly configure a route on server.

We can request the endpoint:

curl http://127.0.0.1:12345/endpoint/

And the response is correct

Problem

But when we request only the headers, the server crash:

curl -I http://127.0.0.1:12345/endpoint/

stub-server is running at http://127.0.0.1:12345
.../node_modules/@pmu-tech/stub-server/dist/cjs/stubServer.js:70
        name = typeof stub.response === 'function' ? stub.response(req) : stub.response;
                           ^

TypeError: Cannot read property 'response' of undefined
    at parseConfig (/node_modules/@pmu-tech/stub-server/dist/cjs/stubServer.js:70:28)
    at processStubRequest (/node_modules/@pmu-tech/stub-server/dist/cjs/stubServer.js:81:28)
    at .../node_modules/@pmu-tech/stub-server/dist/cjs/stubServer.js:130:74
    at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)
    at next (/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)
    at /node_modules/express/lib/router/index.js:281:22
    at param (/node_modules/express/lib/router/index.js:354:14)
    at param (/node_modules/express/lib/router/index.js:365:14)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed.
Exit code: 1
Command: /.nvm/versions/node/v16.6.2/bin/node
Arguments: /usr/local/Cellar/yarn/1.22.0/libexec/lib/cli.js start
Directory: /packages/stubs
Output:

Reason is probably the response is empty.

More info

curl -I option fetch only headers

 -I, --head
              (HTTP FTP FILE) Fetch the headers only! HTTP-servers feature the command HEAD which this  uses  to
              get nothing but the header of a document. When used on an FTP or FILE file, curl displays the file
              size and last modification time only.

Proposal: stateful stub server

Today if we want to preserve the state after some request we need to implement this logic ourselves.
Example:

  • I call POST on /users/add
  • I want to get recently added user on /users
  • I need to implement this storage myself

Instead we can make a simple link between /users/add and /users that will tell stub server that when I make a POST I want to make a simple storage that will return a new user in /users.

In case we need a special treatment we can add transform key as a function that will accept data and return the transformed data that will be stored.

Parse request body

Purpose

In some of cases we would like parse the request body to make some logic.
For example, if body contains some attribute/value I would like return stub A and if it contains another attribute/value I would like return B.

Actual behavior

With this configuration:

    '/test': {
      get: req => {
        console.log('req query', req.query);
        console.log('req body', req.body);
        return `${stubsPath}/test_200_OK.js`;
      }
    }

We have this result:

req query { myQueryParam: 'myValue' }
req body undefined

Desired behaviour

req query { myQueryParam: 'myValue' }
req body { bodyAttr: 'firstAttr' } 

We should consider to use body-parser:

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.