WireMock可以播放来自多个域的请求吗? [英] Can WireMock play back requests from multiple domains?

查看:70
本文介绍了WireMock可以播放来自多个域的请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Dockerized记录回放系统来帮助我记录网站,因此我可以在本地版本而非真实版本的基础上设计刮板.这意味着我不会用自动请求来淹没网站,并且具有我不需要连接到网络即可工作的额外优势.

I am building a Dockerised record-playback system to help me record websites, so I can design scrapers aginst a local version rather than the real thing. This means that I do not swamp a website with automated requests, and has the added advantage that I do not need to be connected to the web to work.

我在内部使用了基于Java的WireMock,它使用Wget从队列中的站点抓取队列中进行记录.我正在使用 WireMock API 从其记录的映射中读取各种片段信息.

I have used the Java-based WireMock internally, which records from a queue of site scrapes using Wget. I am using the WireMock API to read various pieces information from the mappings it records.

但是,我从映射响应中发现似乎未记录域信息(除非偶然在响应标头中记录了域信息).请参见 __ admin/mappings 的以下响应:

However, I have spotted from a mapping response that domain information does not seem to be recorded (except where it is in response headers by accident). See the following response from __admin/mappings:

{

    "result": {
        "ok": true,
        "list": [
            {
                "id": "794d609f-99b9-376d-b6b8-04dab161c023",
                "uuid": "794d609f-99b9-376d-b6b8-04dab161c023",
                "request": {
                    "url": "/robots.txt",
                    "method": "GET"
                },
                "response": {
                    "status": 404,
                    "bodyFileName": "body-robots.txt-j9qqJ.txt",
                    "headers": {
                        "Server": "nginx/1.0.15",
                        "Date": "Wed, 04 Jan 2017 21:04:40 GMT",
                        "Content-Type": "text/html",
                        "Connection": "keep-alive"
                    }
                }
            },
            {
                "id": "e246fac2-f9ad-3799-b7b7-066941408b8b",
                "uuid": "e246fac2-f9ad-3799-b7b7-066941408b8b",
                "request": {
                    "url": "/about/careers/",
                    "method": "GET"
                },
                "response": {
                    "status": 200,
                    "bodyFileName": "body-about-careers-GhVqy.txt",
                    "headers": {
                        "Server": "nginx/1.0.15",
                        "Date": "Wed, 04 Jan 2017 21:04:35 GMT",
                        "Content-Type": "text/html",
                        "Last-Modified": "Wed, 04 Jan 2017 12:52:12 GMT",
                        "Connection": "keep-alive",
                        "X-CACHE-URI": "/about/careers/",
                        "Accept-Ranges": "bytes"
                    }
                }
            },
            {
                "id": "def378f5-a93c-333e-9663-edcd30c936d7",
                "uuid": "def378f5-a93c-333e-9663-edcd30c936d7",
                "request": {
                    "url": "/about/careers/feed/",
                    "method": "GET"
                },
                "response": {
                    "status": 200,
                    "bodyFileName": "body-careers-feed-Fd2fO.xml",
                    "headers": {
                        "Server": "nginx/1.0.15",
                        "Date": "Wed, 04 Jan 2017 21:04:45 GMT",
                        "Content-Type": "application/rss+xml; charset=UTF-8",
                        "Transfer-Encoding": "chunked",
                        "Connection": "keep-alive",
                        "X-Powered-By": "PHP/5.3.3",
                        "Vary": "Cookie",
                        "X-Pingback": "http://www.example.com/xmlrpc.php",
                        "Last-Modified": "Thu, 06 Jun 2013 14:01:52 GMT",
                        "ETag": "\"765fc03186b121a764133349f8b716df\"",
                        "X-Robots-Tag": "noindex, follow",
                        "Link": "<http://www.example.com/?p=2680>; rel=shortlink",
                        "X-CACHE-URI": "null cache"
                    }
                }
            },
            {
                "id": "616ca6d7-6e57-4c10-8b57-f6f3dabc0930",
                "uuid": "616ca6d7-6e57-4c10-8b57-f6f3dabc0930",
                "request": {
                    "method": "ANY"
                },
                "response": {
                    "status": 200,
                    "proxyBaseUrl": "http://www.example.com"
                },
                "priority": 10
            }
        ]
    }

}

URL的唯一清晰记录是在 proxyBaseUrl 的最后一个条目中,并且鉴于我必须

The only clear recording of a URL is in the final entry against proxyBaseUrl, and given that I had to specify a URL in the console call I am now worried that if I record against a different domain, the domain that each one is from will be lost.

这意味着在播放模式下,WireMock将只能从一个域播放,而我必须重新启动它并将其指向另一个缓存才能播放不同的站点.对于我的用例来说,这是行不通的,那么有没有办法解决这个问题?

That would mean that in playback mode, WireMock would only be able to play back from one domain, and I'd have to restart it and point it to another cache in order to play back different sites. This is not workable for my use case, so is there a way around this problem?

((我已经完成了 Mountebank 的一些工作,并且愿意切换到它,尽管我发现WireMock通常更易于使用.我对Mountebank的有限理解是,它同样会遇到相同的单域问题,尽管我很乐意对此进行更正.我很乐意换用任何强大的开源API驱动的驱动程序记录器HTTP代理(如果删除WireMock是前进的唯一方法).

(I have done a little work with Mountebank, and would be willing to switch to it, though I find WireMock generally easier to use. My limited understanding of Mountebank is that it suffers from the same single-domain problem, though I am happy to be corrected on that. I'd be happy to swap to any robust open-source API-driven recorder HTTP proxy, if dropping WireMock is the only way forward).

推荐答案

通过在请求中添加Host标头条件,可以为多个域提供WireMock存根.假设您的DNS/主机文件将所有相关域都映射到WireMock服务器的IP,那么这将使其表现得像普通Web服务器上的虚拟主机一样.

It's possible to serve WireMock stubs for multiple domains by adding a Host header criterion in your requests. Assuming your DNS/host file maps all the relevant domains to your WireMock server's IP, then this will cause it to behave like virtual hosting on an ordinary web server.

主要问题是记录器不会将主机标头添加到您的映射中,因此您以后需要自己执行此操作,或者直接破解记录器以进行操作.

The main issue is that the recorder won't add the host header to your mappings so you'd need to do this yourself afterwards, or hack the recorder to do it on the fly.

我一直在考虑为此提供更好的支持,所以请留意这个空间.

I've been considering adding better support for this, so watch this space.

我还建议您检查一下Hoverfly,它似乎已经很好地解决了这个问题.

I'd also suggest checking out Hoverfly, which seems to solve this problem pretty well already.

这篇关于WireMock可以播放来自多个域的请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆