How to stub some requests and call real service for others using stubby4j

1.2k views Asked by At

I am using stubby4j to stub some service endpoints. I am currently stubbing the ones that are very heavy and not so complex to mock but I would like to call the real service for the rest of the endpoints.

Something like this:

/heavy-call-1 => stub service
/heavy-call-2 => stub service
/lightweight-call-1 => real service
/lightweight-call-2 => real service

Is there a way I can achieve this with this tool or should I consider using a different one?

2

There are 2 answers

1
Luisa Emme On BEST ANSWER

You can actually make stubby call the real service and record the response for the first time, so the next requests will use this recorded response. The way you can do this is by specifying an URL in the body of the stubbed response in your yaml file like this:

-  request:
      url: /1.1/direct_messages.json
      query:
         since_id: 240136858829479935
         count: 1
   response:
      headers:
         content-type: application/json
      body: https://api.twitter.com/1.1/direct_messages.json?since_id=240136858829479935&count=1

You can find some more information in the stubby github docs: https://stubby4j.com/#key-features and https://stubby4j.com/docs/http_endpoint_configuration_howto.html#record-and-replay

Hope this helps!

0
bpereira On

Are you using webpack? If so, you can match different domains. For example:

const config = merge(common, {
  devtool: 'inline-source-map',
  mode: 'development',
  devServer: {
    historyApiFallback: true,
    port: 3000,
    hot: true,
    proxy: [
      { path: '/heavy-all-1 ', target: 'http://localhost:8882' }, //stubby
    ],
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify('development'),
    }),
  ],
});

And the URLs that don't have the prefix described won't be stubbed.