Status code of 426 is returned instead of 200 when getting a simple endpoint from an Express app (in localhost) using Karate

huangapple go评论54阅读模式
英文:

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 &#39;http://localhost:3000&#39;

  Scenario: Read users
    Given path &#39;users&#39;
    And header Upgrade = &#39;HTTP/1.0&#39;
    And header Connection = &#39;Upgrade&#39;
    And header Accept = &#39;application/json&#39;
    And header Accept-Encoding = &#39;gzip, deflate&#39;
    And header Connection = &#39;keep-alive&#39;
    When method get
    Then status 200


    * print response
    * print response.headers

I only have one dependency in pom.xml:

&lt;dependency&gt;
            &lt;groupId&gt;com.intuit.karate&lt;/groupId&gt;
            &lt;artifactId&gt;karate-junit5&lt;/artifactId&gt;
            &lt;version&gt;1.4.1.RC2&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;

The express app:

const port = 3000

    var testData = [
            {
              &quot;name&quot;: &quot;John Mayer&quot;,
              &quot;info&quot;: {
               &quot;title&quot;: &quot;Mr.&quot;,
               &quot;age&quot;: &quot;35&quot;,
               &quot;phone&quot;: &quot;23423423-23423&quot;,
                &quot;email&quot;: &quot;jmayer@test.com&quot;
            },
            &quot;id&quot;:&quot;old001&quot;
        }
    ]
    
    app.listen(port, () =&gt; {
        console.log(`Server listening on port ${port}`)
    })
    
    
    app.get(&#39;/users&#39;, (req, res) =&gt; {
        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.

huangapple
  • 本文由 发表于 2023年7月10日 11:03:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定