2017-07-18 83 views
0

我正在使用spring引导并希望创建并测试Http服务。 HTTP服务如下:为什么模拟服务器(restito)返回nginx 302?

@Service 
public class HttpService { 

    private Client client; 

    @Autowired 
    public HttpService(Client client) { 
    this.client = client; 
    } 

    <T> T get(String url, MultivaluedMap params, Class<T> type) { 

    ClientResponse response = 
     client.resource(url) 
       .queryParams(params) 
       .accept(MediaType.APPLICATION_JSON_TYPE) 
       .type(MediaType.APPLICATION_JSON_TYPE) 
       .get(ClientResponse.class); 

    return response.getEntity(type); 
    } 
} 

和我的测试如下:

public class HttpServiceTests { 

    private StubServer server; 

    @Before 
    public void start() { 
    server = new StubServer().run(); 
    RestAssured.port = server.getPort(); 
    } 

    @After 
    public void stop() { 
    server.stop(); 
    } 

    @Test 
    public void answerWith200() { 
    ClientConfig clientConfig = new DefaultClientConfig(); 
    clientConfig.getClasses().add(JacksonJaxbJsonProvider.class); 
    clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); 

    Client client = Client.create(clientConfig); 
    HttpService httpService = new HttpService(client); 

    whenHttp(server).match(get("http://test.com")).then(status(HttpStatus.OK_200), stringContent("all ok")); 

    String url = "http://test.com"; 
    String response = httpService.get(url, new MultivaluedMapImpl(), String.class); 
    Assert.assertEquals("all ok", response); 
    } 
} 

运行测试时的实际反应是:

<html> 
<head><title>302 Found</title></head> 
<body bgcolor="white"> 
<center><h1>302 Found</h1></center> 
<hr><center>nginx/1.11.13</center> 
</body> 
</html> 

没有人有任何想法,为什么这是怎么回事?当我尝试使用Jadler库时,我也得到了相同的响应。

回答

0

这是因为你的客户打一些Nginx的服务器,而不是由模拟框架开始服务。

检查restito examples更仔细地看需要,使其与Restito服务器,而不是一些真正的应用程序进行交互传递到您的客户端,它的网址。

还要注意,你并不需要的协议和域名进入whenHttp(server).match(get("..."))

相关问题