I'm running a Karate test suite to send a POST request to a mock server running locally on http://localhost:8080/api/messages. The mock server is implemented using Karate itself. While the request works perfectly in Postman, it fails in Karate with the error:
00:06:12.347 [pool-1-thread-1] ERROR com.intuit.karate - java.net.SocketException: Broken pipe, http call failed after 165 milliseconds for url: http://localhost:8080/api/messages
00:06:12.348 [pool-1-thread-1] ERROR com.intuit.karate - classpath:resources/steps/mailosaur/send_sms.feature:12
When method POST
http call failed after 165 milliseconds for url: http://localhost:8080/api/messages
java.net.SocketException: Broken pipe
classpath:resources/steps/mailosaur/send_sms.feature:12
When sending the request via Postman, the server responds as expected:
{
"success": true
}
Here’s the equivalent cURL command from Postman:
curl --location --request POST 'http://localhost:8080/api/messages' \
--header 'Content-Type: application/json' \
--data '{"to":"+15551234444","text":"#1234 info"}'
Here is the test feature for sending the SMS:
@send_sms
Feature: Send SMS
Background:
* header Accept = 'application/json'
Scenario: Send an SMS
Given url 'http://localhost:8080/api/messages'
And request { "to": "+15551234444", "text": "#1234 info" }
When method POST
Then status 200
And match response.success == true
The mock server is implemented in Karate:
Feature: Mock Server for SMS Testing
Background:
* def messages = []
Scenario: pathMatches('/api/messages') && methodIs('post')
* def requestBody = request
* messages.add({ to: requestBody.to, text: requestBody.text })
* def response = { success: true }
* def responseStatus = 200
The mock server starts successfully and matches the request:
23:57:06.953 [main] INFO com.intuit.karate - mock server initialized: src/test/java/resources/steps/mock/sms-mock.feature
23:57:07.033 [main] DEBUG com.intuit.karate.http.HttpServer - server started: mock-0082.local:8080
Karate Version: 1.4.1 JDK Version: OpenJDK 21
I think you have a syntax error in the mock. In the world of JavaScript, push()
is how to add an item to an array. It is different from the Java add()
which can be confusing.
Make this change:
Scenario: pathMatches('/api/messages') && methodIs('post')
* messages.push({ to: request.to, text: request.text })
* print messages
* def response = { success: true }
I can't explain why Postman worked and the broken pipe
is probably normal when an HTTP server fails.
To test, I ran this feature pointing to your mock:
Feature:
Scenario:
* karate.start({ mock: 'mock.feature', port: 8080 })
Given url 'http://localhost:8080/api/messages'
And request { "to": "+15551234444", "text": "#1234 info" }
When method post
Then status 200
And match response.success == true