简体   繁体   English

如何使用 stubby4j 存根一些请求并为其他人调用真正的服务

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

I am using stubby4j to stub some service endpoints.我正在使用stubby4j存根一些服务端点。 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?有没有办法用这个工具来实现这一点,还是应该考虑使用不同的工具?

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.您实际上可以使 stubby 调用真正的服务并第一次记录响应,因此接下来的请求将使用此记录的响应。 The way you can do this is by specifying an URL in the body of the stubbed response in your yaml file like this:您可以通过在 yaml 文件中的存根响应正文中指定一个 URL 来执行此操作,如下所示:

-  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/#record-and-replay您可以在 stubby github 文档中找到更多信息: https : //stubby4j.com/#key-featureshttps://stubby4j.com/#record-and-replay

Hope this helps!希望这可以帮助!

Are you using webpack?你在使用 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.没有描述的前缀的 URL 不会被存根。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM