Karate Test Fails with “Broken pipe” While Sending POST Request to Mock Server

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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

Postman Behavior

When sending the request via Postman, the server responds as expected:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"success": true
}
</code>
<code>{ "success": true } </code>
{
  "success": true
}

Here’s the equivalent cURL command from Postman:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>curl --location --request POST 'http://localhost:8080/api/messages'
--header 'Content-Type: application/json'
--data '{"to":"+15551234444","text":"#1234 info"}'
</code>
<code>curl --location --request POST 'http://localhost:8080/api/messages' --header 'Content-Type: application/json' --data '{"to":"+15551234444","text":"#1234 info"}' </code>
curl --location --request POST 'http://localhost:8080/api/messages' 
--header 'Content-Type: application/json' 
--data '{"to":"+15551234444","text":"#1234 info"}'

Karate Test Code

Here is the test feature for sending the SMS:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@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
</code>
<code>@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 </code>
@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

Mock Server Details

The mock server is implemented in Karate:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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

Environment

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Scenario: pathMatches('/api/messages') && methodIs('post')
* messages.push({ to: request.to, text: request.text })
* print messages
* def response = { success: true }
</code>
<code>Scenario: pathMatches('/api/messages') && methodIs('post') * messages.push({ to: request.to, text: request.text }) * print messages * def response = { success: true } </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật