我构建了一个带有http://sparkjava.com/
的http服务器,用于验证文件是否有效。
代码非常简单:
post("/pdf", (req, res) -> {
if (req.headers("application/pdf") == null) {
res.status(400);
PdfState state = new PdfState();
state.SetIsValid(false);
return JsonUtil.toJson(state);
}
InputStream stream = new ByteArrayInputStream(req.bodyAsBytes());
PdfState state = pdf.validate(stream);
res.type("application/json");
return JsonUtil.toJson(state);
});
然后我想编写单元测试并选择http客户端库http://square.github.io/okhttp/。
测试功能如下:
public void IsPdfContentRequestValid_StreamValidPdfContent_ExpectJsonSuccess() throws Exception {
File file = new File("/Volumes/Dev/java/inspector/files/pass.pdf");
Request request = new Request.Builder()
.url("http://127.0.0.1:3000/pdf")
.addHeader("Content-type", "application/pdf")
.post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
当我运行测试时,它会抱怨:
java.net.ConnectException: Failed to connect to /127.0.0.1:3000
OKClient有什么问题?
更新
这就是整个错误:
[TestNG] Running:
/Users/developer/Library/Caches/IntelliJIdea2016.2/temp-testng-customsuite.xml
0 [Thread-1] INFO org.eclipse.jetty.util.log - Logging initialized @1554ms
java.net.ConnectException: Failed to connect to /127.0.0.1:3000
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:189)
at okhttp3.internal.connection.RealConnection.buildConnection(RealConnection.java:173)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:114)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:193)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:129)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:98)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:109)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170)
at okhttp3.RealCall.execute(RealCall.java:60)
at com.cloudfirmation.inspector.controller.PdfControllerTest.IsPdfContentRequestValid_StreamValidPdfContent_ExpectJsonSuccess(PdfControllerTest.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1129)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:746)
at org.testng.TestRunner.run(TestRunner.java:600)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1264)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1189)
at org.testng.TestNG.runSuites(TestNG.java:1104)
at org.testng.TestNG.run(TestNG.java:1076)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:124)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
56 [main] INFO spark.embeddedserver.jetty.EmbeddedJettyServer - >>> Spark shutting down ...
56 [main] INFO spark.embeddedserver.jetty.EmbeddedJettyServer - done
===============================================
Default Suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
我认为服务器在开始测试时正在运行。