我想为以下e2e场景添加一个测试:
我的应用程序通过内部代理服务器向外部服务发出web请求,代理服务器操作请求主体,将请求转发给目的主机,并返回返回的响应。
例如,我向external.service/an/endpoint
(通过my-proxy-server
)发送请求。
{
"card_number": "<proxy server pls fill the cc details>"
}
代理服务器修改请求以填充 cc 详细信息,并将其转发到带有正文的外部.service/an/终结点
{
"card_number": "372735466563005"
}
外部。服务返回状态OK。代理服务器返回响应而不进行修改。
如何使用线束测试此工作流?我可以为外部服务
做WireMock.stubFor(),
但我不知道如何使线对代理与我的网络客户端的代理设置一起工作。看到测试,实际上,Rest模板测试,rest模板使用WireMockAs代理服务器
按预期工作,通过代理路由我的请求,但是web客户端与
我的RCA一起路由我的模拟服务器错误:
20:06:59.165 [qtp105751207-24] DEBUG wiremock.org.eclipse.jetty.server.HttpChannel - REQUEST for //localhost:58978localhost:58978 on HttpChannelOverHttp@4a71ab50{r=1,c=false,c=false/false,a=IDLE,uri=//localhost:58978localhost:58978,age=0}
CONNECT //localhost:58978localhost:58978 HTTP/1.1
Host: localhost:58978
通过电线代理进行这些调用是不可能的,如此处所述。但是我所有的网址都像 http://localhost:
package com.spotnana.obt.supplier.services;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.http.RequestMethod;
import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.nio.charset.StandardCharsets;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHeaders;
import org.apache.http.conn.HttpHostConnectException;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.http.client.reactive.ReactorResourceFactory;
import org.springframework.util.SocketUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import reactor.netty.tcp.ProxyProvider;
@Slf4j
public class SimpleWiremockProxyServerTest {
private final String HOST = "localhost";
private final String MOCK_ENDPOINT = "/my/endpoint";
private WireMockServer targetServer;
private WireMockServer proxyServer;
private WireMock targetWireMock;
private WireMock proxyWireMock;
private String targetBaseUrl;
@Before
public void setup() {
final int targetPort = SocketUtils.findAvailableTcpPort();
this.targetServer = new WireMockServer(WireMockConfiguration.wireMockConfig().port(targetPort));
this.targetServer.start();
this.targetWireMock = new WireMock(targetPort);
this.targetWireMock.resetMappings();
this.targetBaseUrl = "http://" + HOST + ":" + targetPort;
final int proxyPort = SocketUtils.findAvailableTcpPort();
this.proxyServer =
new WireMockServer(
WireMockConfiguration.wireMockConfig().port(proxyPort).enableBrowserProxying(true));
this.proxyServer.start();
this.proxyWireMock = new WireMock(proxyPort);
this.proxyWireMock.resetMappings();
}
@After
public void tearDown() throws HttpHostConnectException {
this.targetWireMock.shutdown();
this.targetServer.stop();
try {
this.proxyWireMock.shutdown();
this.proxyServer.stop();
} catch (final Exception ex) {
log.warn("Proxy server is shutdown already");
}
}
@Test
public void restTemplateWithWireMockAsProxyServer() {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(HOST, this.proxyServer.port()));
requestFactory.setProxy(proxy);
final var reqPatternBuilder =
RequestPatternBuilder.newRequestPattern(
RequestMethod.GET, WireMock.urlEqualTo(MOCK_ENDPOINT));
final var mappingBuilder =
WireMock.get(WireMock.urlEqualTo(reqPatternBuilder.build().getUrl()));
reqPatternBuilder
.withHeader(HttpHeaders.ACCEPT, WireMock.containing(MediaType.APPLICATION_JSON_VALUE))
.withHeader(
HttpHeaders.ACCEPT_CHARSET,
WireMock.containing(StandardCharsets.UTF_8.name().toUpperCase()));
mappingBuilder.willReturn(
WireMock.aResponse()
.withStatus(HttpStatus.OK.value())
.withBody("{ \"success\": true }")
.withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE));
this.targetWireMock.register(mappingBuilder);
ResponseEntity<String> responseEntity =
new RestTemplate(requestFactory)
.getForEntity(this.targetBaseUrl + MOCK_ENDPOINT, String.class);
Assert.assertEquals(responseEntity.getStatusCode(), HttpStatus.OK);
System.out.println("responseEntity: " + responseEntity.getBody());
}
@Test
public void webClientWithWireMockAsProxyServer() {
var client = HttpClient.create()
.tcpConfiguration(
tcpClient ->
tcpClient.proxy(
proxy -> {
proxy
.type(ProxyProvider.Proxy.HTTP)
.host(HOST)
.port(this.proxyServer.port());
}));
var webClient = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(client))
.build();
final var reqPatternBuilder =
RequestPatternBuilder.newRequestPattern(
RequestMethod.GET, WireMock.urlEqualTo(MOCK_ENDPOINT));
final var mappingBuilder =
WireMock.get(WireMock.urlEqualTo(reqPatternBuilder.build().getUrl()));
reqPatternBuilder
.withHeader(HttpHeaders.ACCEPT, WireMock.containing(MediaType.APPLICATION_JSON_VALUE))
.withHeader(
HttpHeaders.ACCEPT_CHARSET,
WireMock.containing(StandardCharsets.UTF_8.name().toUpperCase()));
mappingBuilder.willReturn(
WireMock.aResponse()
.withStatus(HttpStatus.OK.value())
.withBody("{ \"success\": true }")
.withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE));
this.targetWireMock.register(mappingBuilder);
var response =
webClient.get().uri(this.targetBaseUrl + MOCK_ENDPOINT).exchange().block().bodyToMono(String.class);
response.subscribe(x -> System.out.println("x:" + x));
}
}
我报错< code > Java . net . unknownhostexception:
Wiremock不支持HTTP CONNECT方法。您可以尝试Hoverfly作为Wiremock的替代品。如果您对详细信息感兴趣,则存在github问题。
刚开始使用wiremock,遇到了一个场景,我想用一个特定的json响应来存根GET请求。 将json附加到预期响应时; 我得到例外java.io.FileNotFoundException: src/test/资源/__files/product.json(没有这样的文件或目录)。 问题是我在这个位置有json文件。
Spring Cloud Contract提供了一个方便的类,可以将JSON WireMock存根加载到Spring MockRestServiceServer中。以下是一个例子: @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.NONE) public class WiremockForDo
我已经在本地下载了wiremock独立jar。我使用下面的命令启动独立服务器。java-jar wiremock-jre8-standalone-2.26.3.jar--端口8089 我需要一个可以连接到我的wiremock服务器并显示所有映射的用户界面。如果UI能够提供永久编辑、删除和添加新文件的特征,这将是有利的。
我有以下服务: 和其他服务 我有我的Junit 当我在调试模式下运行这个测试时,我看到这个。合同服务。getInfo(multipartFileId) 正在返回我“null”。 我在嘲弄中错在哪里。 我刚刚在JUnit中嘲笑了ContractService。我还需要模拟AccountServiceImpl吗? 编辑:添加saveInCache和getInfo方法
问题内容: 我一直在使用selenium在python中自动进行浏览器模拟和Web抓取,对我来说效果很好。但是现在,我必须在代理服务器后运行它。现在,selenium打开了窗口,但是由于未在打开的浏览器中设置代理设置而无法打开请求的页面。当前代码如下(示例): 如何更改上面的代码以立即与代理服务器一起使用? 问题答案: 您需要设置所需的功能或浏览器配置文件,如下所示:
行动时刻 - 使用虚拟服务器 请按照以下步骤使虚拟服务器可用: 1.编辑FreeRADIUS配置目录中的radiusd.conf文件,并将以下内容添加到包含type = auth的listen部分(有两个listen部分,一个有type = auth,另一个有type = acct): virtual_server = always_accept。 2.在调试模式下重新启动FreeRADIUS。