@Test public void noResponseBodyForPut() { server.enqueue(new MockResponse()); TestInterface api = Feign.builder().target(TestInterface.class, "http://localhost:" + server.getPort()); api.noPutBody(); }
/** * We currently don't include the <a href="http://java.net/jira/browse/JERSEY-639">60-line * workaround</a> jersey uses to overcome the lack of support for PATCH. For now, prefer okhttp. * * @see java.net.HttpURLConnection#setRequestMethod */ @Test public void patchUnsupported() throws IOException, InterruptedException { thrown.expectCause(isA(ProtocolException.class)); TestInterface api = Feign.builder().target(TestInterface.class, "http://localhost:" + server.getPort()); api.patch(); }
/** Test of get method, of class SAMPFactory. */ public void testGet_SAMPMessage_Class() throws Exception { String mtype = "test"; Object instance = getInstance(); SAMPMessage message = SAMPFactory.createMessage(mtype, instance, TestInterface.class); TestInterface result = (TestInterface) SAMPFactory.get(message, TestInterface.class); assertEquals("Test Name", result.getName()); assertTrue(Arrays.equals(new double[] {1., 2., 3.}, result.getArray())); assertEquals("Test Something", result.getNested().getSomething()); }
@Test public void parsesErrorResponse() throws IOException, InterruptedException { thrown.expect(FeignException.class); thrown.expectMessage("status 500 reading TestInterface#get(); content:\n" + "ARGHH"); server.enqueue(new MockResponse().setResponseCode(500).setBody("ARGHH")); TestInterface api = Feign.builder().target(TestInterface.class, "http://localhost:" + server.getPort()); api.get(); }
@Test public void canOverrideSSLSocketFactory() throws IOException, InterruptedException { server.useHttps(TrustingSSLSocketFactory.get("localhost"), false); server.enqueue(new MockResponse()); TestInterface api = Feign.builder() .client(trustSSLSockets) .target(TestInterface.class, "https://localhost:" + server.getPort()); api.post("foo"); }
@Test public void reasonPhraseIsOptional() throws IOException, InterruptedException { server.enqueue(new MockResponse().setStatus("HTTP/1.1 " + 200)); TestInterface api = Feign.builder().target(TestInterface.class, "http://localhost:" + server.getPort()); Response response = api.post("foo"); assertThat(response.status()).isEqualTo(200); assertThat(response.reason()).isNull(); }
@Test public void canOverrideHostnameVerifier() throws IOException, InterruptedException { server.useHttps(TrustingSSLSocketFactory.get("bad.example.com"), false); server.enqueue(new MockResponse()); TestInterface api = Feign.builder() .client(disableHostnameVerification) .target(TestInterface.class, "https://localhost:" + server.getPort()); api.post("foo"); }
@Test public void createPListConfiguration() { StartupModule startup = StartupModule.create( ASMClasspathScanner.class, PackageFilter.create(DirectFileConfigTests.class)); startup.addFeature(ConfigurationFeature.class); Injector injector = Guice.createInjector(startup); assertNotNull(injector); TestInterface instance = injector.getInstance(TestInterface.class); Assert.assertTrue(instance.sayHello(), "sayHello() - yeahh!!".equals(instance.sayHello())); }
@Test public void retriesFailedHandshake() throws IOException, InterruptedException { server.useHttps(TrustingSSLSocketFactory.get("localhost"), false); server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE)); server.enqueue(new MockResponse()); TestInterface api = Feign.builder() .client(trustSSLSockets) .target(TestInterface.class, "https://localhost:" + server.getPort()); api.post("foo"); assertEquals(2, server.getRequestCount()); }
/** This shows that is a no-op or otherwise doesn't cause an NPE when there's no content. */ @Test public void safeRebuffering_noContent() throws IOException, InterruptedException { server.enqueue(new MockResponse().setResponseCode(204)); TestInterface api = Feign.builder() .logger( new Logger() { @Override protected void log(String configKey, String format, Object... args) {} }) .logLevel(Logger.Level.FULL) // rebuffers the body .target(TestInterface.class, "http://localhost:" + server.getPort()); api.post("foo"); }
@Test public void testHTTPSViaRibbon() { Client trustSSLSockets = new Client.Default(TrustingSSLSocketFactory.get(), null); server1.get().useHttps(TrustingSSLSocketFactory.get("localhost"), false); server1.enqueue(new MockResponse().setBody("success!")); getConfigInstance().setProperty(serverListKey(), hostAndPort(server1.getUrl(""))); TestInterface api = Feign.builder() .client(RibbonClient.builder().delegate(trustSSLSockets).build()) .target(TestInterface.class, "https://" + client()); api.post(); assertEquals(1, server1.getRequestCount()); }
@Test public void ioExceptionRetry() throws IOException, InterruptedException { server1.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_START)); server1.enqueue(new MockResponse().setBody("success!")); getConfigInstance().setProperty(serverListKey(), hostAndPort(server1.getUrl(""))); TestInterface api = Feign.builder() .client(RibbonClient.create()) .target(TestInterface.class, "http://" + client()); api.post(); assertEquals(2, server1.getRequestCount()); // TODO: verify ribbon stats match // assertEquals(target.lb().getLoadBalancerStats().getSingleServerStat()) }
public void testTargetNonSelectionForMismatchedType() { testImpl2.interfaceMethod(); assertEquals( "Shouldn't have been advised by POJO advice for impl", 0, testAspectForTestImpl1.count); assertEquals( "Should have been advised by POJO advice for base type", 1, testAspectForAbstractTestImpl.count); assertEquals("Shouldn't have been advised by advisor", 0, testInterceptor.count); }
/* This test-case replicates a bug that occurs when using RibbonRequest with a query string. The querystrings would not be URL-encoded, leading to invalid HTTP-requests if the query string contained invalid characters (ex. space). */ @Test public void urlEncodeQueryStringParameters() throws IOException, InterruptedException { String queryStringValue = "some string with space"; String expectedQueryStringValue = "some+string+with+space"; String expectedRequestLine = String.format("GET /?a=%s HTTP/1.1", expectedQueryStringValue); server1.enqueue(new MockResponse().setBody("success!")); getConfigInstance().setProperty(serverListKey(), hostAndPort(server1.getUrl(""))); TestInterface api = Feign.builder() .client(RibbonClient.create()) .target(TestInterface.class, "http://" + client()); api.getWithQueryParameters(queryStringValue); final String recordedRequestLine = server1.takeRequest().getRequestLine(); assertEquals(recordedRequestLine, expectedRequestLine); }
@Test public void parsesRequestAndResponse() throws IOException, InterruptedException { server.enqueue(new MockResponse().setBody("foo").addHeader("Foo: Bar")); TestInterface api = Feign.builder().target(TestInterface.class, "http://localhost:" + server.getPort()); Response response = api.post("foo"); assertThat(response.status()).isEqualTo(200); assertThat(response.reason()).isEqualTo("OK"); assertThat(response.headers()) .containsEntry("Content-Length", asList("3")) .containsEntry("Foo", asList("Bar")); assertThat(response.body().asInputStream()) .hasContentEqualTo(new ByteArrayInputStream("foo".getBytes(UTF_8))); assertThat(server.takeRequest()) .hasMethod("POST") .hasPath("/?foo=bar&foo=baz&qux=") .hasHeaders("Foo: Bar", "Foo: Baz", "Qux: ", "Accept: */*", "Content-Length: 3") .hasBody("foo"); }
/** Test of get method, of class SAMPFactory. */ public void testGet_Class() { TestInterface result = (TestInterface) SAMPFactory.get(TestInterface.class); List<String> methods = new ArrayList<>(); List<String> exp_methods = new ArrayList<>(); for (Method m : result.getClass().getDeclaredMethods()) methods.add(m.getName()); for (Method m : TestInterface.class.getDeclaredMethods()) exp_methods.add(m.getName()); assertTrue(methods.containsAll(exp_methods)); result.setName("test name"); result.setArray(new double[] {0.1, 0.2}); result.getNested().setSomething("test something"); assertEquals("test name", result.getName()); assertTrue(Arrays.equals(new double[] {0.1, 0.2}, result.getArray())); assertEquals("test something", result.getNested().getSomething()); result.addThing("Three"); assertTrue(result.getThings().contains("Three")); }
@Test public void mockAllClassesImplementingAnInterfaceUsingNamedMockUpWithInvocationParameter() { TestInterface impl1 = new TestInterface() { @Override public String getData() { return "1"; } }; TestInterface impl2 = new TestInterface() { @Override public String getData() { return "2"; } }; new MockTestInterface(); String mocked1 = impl1.getData(); String mocked2 = impl2.getData(); assertEquals("mocked1", mocked1); assertEquals("mocked2", mocked2); }