英文:
Status code of 426 is returned instead of 200 when getting a simple endpoint from an Express app (in localhost) using Karate
问题
我已经编写了一个简单的Express应用程序。我正在尝试使用Karate进行API测试,当我尝试获取某个端点(字面意思上是GET)时,我期望得到状态码200以及该端点的JSON测试数据作为响应。然而,我却得到了426的响应码。
以下是Karate特性文件:
Feature: Test
Background:
* url 'http://localhost:3000'
Scenario: Read users
Given path 'users'
And header Upgrade = 'HTTP/1.0'
And header Connection = 'Upgrade'
And header Accept = 'application/json'
And header Accept-Encoding = 'gzip, deflate'
And header Connection = 'keep-alive'
When method get
Then status 200
* print response
* print response.headers
我的pom.xml中只有一个依赖项:
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-junit5</artifactId>
<version>1.4.1.RC2</version>
<scope>test</scope>
</dependency>
Express应用程序的代码如下:
const port = 3000
var testData = [
{
"name": "John Mayer",
"info": {
"title": "Mr.",
"age": "35",
"phone": "23423423-23423",
"email": "jmayer@test.com"
},
"id": "old001"
}
]
app.listen(port, () => {
console.log(`Server listening on port ${port}`)
})
app.get('/users', (req, res) => {
console.log(req.protocol)
res.status(200).send(testData)
})
当我在Postman中尝试时,我得到了状态码200和预期的JSON测试数据。
我还尝试在Karate特性中添加了Upgrade和Connection头,但仍然得到了426状态码。
这是我收到的响应的屏幕截图。
426响应和Server: Websocket++/0.70
'Server: Websocket++/0.70'看起来有点奇怪,但我不确定它的来源在哪里。
英文:
I have written a simple Express app. I am trying out Karate for API Testing, when I try to get a certain endpoint (literally GET) im expecting a response of status code 200 and the json test data for that endpoint. However, I am getting a 426 instead.
Here is the karate feature file
Feature: Test
Background:
* url 'http://localhost:3000'
Scenario: Read users
Given path 'users'
And header Upgrade = 'HTTP/1.0'
And header Connection = 'Upgrade'
And header Accept = 'application/json'
And header Accept-Encoding = 'gzip, deflate'
And header Connection = 'keep-alive'
When method get
Then status 200
* print response
* print response.headers
I only have one dependency in pom.xml:
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-junit5</artifactId>
<version>1.4.1.RC2</version>
<scope>test</scope>
</dependency>
The express app:
const port = 3000
var testData = [
{
"name": "John Mayer",
"info": {
"title": "Mr.",
"age": "35",
"phone": "23423423-23423",
"email": "jmayer@test.com"
},
"id":"old001"
}
]
app.listen(port, () => {
console.log(`Server listening on port ${port}`)
})
app.get('/users', (req, res) => {
console.log(req.protocol)
res.status(200).send(testData)
})
When I try this with Postman I get a 200 and the expected json test data.
I tried adding the Upgrade and Connection headers in the Karate feature too but I'm still getting a 426 status code.
Here is the screenshot of the response im getting.
426 response and server:websocket
The 'Server: Websocket++/0.70' looks odd but I am unsure where that originates from.
答案1
得分: 1
更改端口(从3000更改为3500)解决了我的问题。
之前我在使用工作机器时遇到了这个问题,可能是由于代理/防病毒软件导致的,这也可以解释为什么我没有得到正确的状态代码,正如@PeterThomas所指出的那样。更改端口解决了这个问题。
英文:
Update/Solution :
Changing the port (from 3000 to 3500) solved it for me.
I was encountering the issue earlier whilst using a work machine with proxy/anti-virus which may explain why I was not getting the proper status codes as @PeterThomas has also pointed out. Changing the port solved the issue.
答案2
得分: 0
首先尝试移除您Karate测试中的所有标头。
如果仍然不起作用,请添加回Accept
标头。
英文:
First try removing all the headers in your Karate test.
If it still doesn't work, add back the Accept
header.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论