我从头创建了一个grails应用程序,使用API创建一个基本的测试应用程序。一切都运行良好,当我在现有的Grails应用程序中放入完全相同的代码时,我在运行JUnits时遇到以下异常。
java.lang.IllegalArgumentException: Illegal character(s) in message header value: Basic MWU3ZWY3NDg3YzNlMzRiMDk3N2YyNzNiZGRmZTE3NTQ6
at sun.net.www.protocol.http.HttpURLConnection.checkMessageHeader(HttpURLConnection.java:482)
at sun.net.www.protocol.http.HttpURLConnection.isExternalMessageHeaderAllowed(HttpURLConnection.java:434)
at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:2752)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.setRequestProperty(HttpsURLConnectionImpl.java:316)
at de.paymill.net.HttpClient.setAuthentication(HttpClient.java:230)
at de.paymill.net.HttpClient.createConnection(HttpClient.java:212)
at de.paymill.net.HttpClient.request(HttpClient.java:143)
at de.paymill.net.HttpClient.post(HttpClient.java:100)
at de.paymill.service.PaymentService.create(PaymentService.java:31)
at de.paymill.service.PaymentService$create.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at com.linquire.mib.service.PaymentsService.createPaymentMethod(PaymentsService.groovy:113)
at com.linquire.mib.service.PaymentsServiceIntegrationTests.createPaymentsTest(PaymentsServiceIntegrationTests.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
这是在现有应用程序中实现的测试。
@Test
public void createPaymentsTest() {
System.out.println("the private api key now is: "+Paymill.getApiKey());
String token = (String) paymentsService.getToken("4111111111111111", "123", "12", "2013");
System.out.println(token);
paymentsService.createPaymentMethod(token);
assertNotNull(userService.getPaymentMethod());
assertEquals("1111", userService.getPaymentMethodLast4());
assertEquals("visa", userService.getPaymentMethodCardType());
}
以下是测试应用程序中的代码。
@Test
public void createPaymentsTest() {
System.out.println("the private api key now is: "+Paymill.getApiKey());
String token = (String) makePaymentService.getToken("4111111111111111", "123", "12", "2013");
System.out.println(token);
Payment p = (Payment) makePaymentService.createPayment(token);
System.out.println();
assertEquals("1111", p.getLast4());
assertEquals("visa", p.getCardType());
}
正如您所看到的,代码非常相似。 createPayment(token)函数只调用一个调用API函数的函数。 在现有应用程序中的实现是:
def createPaymentMethod(String token) {
PaymentService paymentService = Paymill.getService(PaymentService.class);
Payment payment = paymentService.create(token);
//Sets the PaymentMethodId field for the current user
userService.setPaymentMethod(payment.getId());
}
测试应用程序中的相同功能是:
def createPayment(String token) {
PaymentService paymentService = Paymill.getService(PaymentService.class);
Payment payment = paymentService.create(token);
return paymentService.get(payment.id);
}
我已经看过config.groovy等可能存在的差异,但我似乎无法想到为什么当它完全相同的代码(甚至传递相同的参数等)时它会表现不同的原因< / p>
从我所看到的,这个错误通常是由于在HTTP请求中的某处插入CRLF字符而发生的,但是我真的不能想到现有应用程序中的某些组件在发送请求时可能会引入这样的字符。 如果有人对可能导致此问题的原因有所了解,请告诉我。
感谢。
答案 0 :(得分:5)
由于用户名/密码转换为base64的错误,通常会发现该错误:See here。
与拥有短密码和用户名的用户尝试相同的通话,以查看问题是否消失。
答案 1 :(得分:3)
确保您的requestXML或任何标头值没有任何非法字符,并将其替换为......
aXmlRequest=aXmlRequest.replaceAll("\n", "");