空手道API测试-我们如何从Content-Type:text/html类型的响应中提取值? [英] Karate API Testing - How do we extract values from Content-Type: text/html kind of response?

查看:66
本文介绍了空手道API测试-我们如何从Content-Type:text/html类型的响应中提取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个请求,其中包括主体中的graphQL查询.

I have a request which includes graphQL query in body.

当我发布它时,它返回带有 Content-Type→text/html;的响应;charset = utf-8 和其他一些常规标头. txt/html 响应通常包括两件事:

When I post it, it returns a response with Content-Type →text/html; charset=utf-8 and some other Usual headers. The txt/html response includes usually two things:

  1. ColDefs作为第一行
  2. 第二行中的元数据

现在,我需要验证ColDefs始终出现在第一行中.第二行包含元数据.

Now I need to validate that ColDefs appears always in the first row. The second row contains metadata.

如何确保响应的第一行始终包含colDefs,第二行包含元数据信息?

How do I make sure that the response first row always contains colDefs and second-row contains metadata info?

回复:

{
    "colDefs": [{
        "entityAttributeId": "abc",
        "headerName": "xyz",
        "field": "2",
        "entityPath": "",
        "entityId": "mna"
    }, {
        "entityAttributeId": "abc",
        "headerName": "abc",
        "field": "3",
        "entityPath": "abc",
        "entityId": "abc"
    }
        
]
} {
    "1": "1000",
    "2": "abc",
    "3": "abc",
    "4": 12,
    "5": "6457.T",
    "6": "123",
    "7": "abc"
} {
    "1": "123",
    "2": "abc",
    "3": "abc",
    "4": 123,
    "5": "123",
    "6": "",
    "7": "abc"
} 

当我尝试打印空手道响应时,它不是在打印第二行(元数据)内容,而只是在打印colDefs.

When I try to print, the karate response, it is not printing second-row(metadata) contents, it is just printing colDefs.

空手道回应:

{
    "colDefs": [{
        "entityAttributeId": "abc",
        "headerName": "xyz",
        "field": "2",
        "entityPath": "",
        "entityId": "mna"
    }, {
        "entityAttributeId": "abc",
        "headerName": "abc",
        "field": "3",
        "entityPath": "abc",
        "entityId": "abc"
}

要复制的步骤:

URL:一些URL

URL: some URL

Headers: Content-Type = 'application/json'

(在空手道功能中明确发送),

(sending explicitly in karate feature ) ,

请求正文

{
"query": "query($someid: [String]) {some(someid:$someid) {someid someNm someVariable {someVariable someVariableid otherVariable{ otherVariable1 { variable CUSIP issuer { someVariable2 }}}}}}",
        "variables": {
            "someid": ["1090"]
        },
        "includeMetadata": false
    }       
    
Response Header:  Connection →keep-alive
Content-Length →86488
Content-Type →text/html; charset=utf-8
   

响应正文:

{
    "colDefs": [{
        "entityAttributeId": "abc",
        "headerName": "xyz",
        "field": "2",
        "entityPath": "",
        "entityId": "mna"
    }, {
        "entityAttributeId": "abc",
        "headerName": "abc",
        "field": "3",
        "entityPath": "abc",
        "entityId": "abc"
    }
        
]
} {
    "1": "1000",
    "2": "abc",
    "3": "abc",
    "4": 12,
    "5": "6457.T",
    "6": "123",
    "7": "abc"
} {
    "1": "123",
    "2": "abc",
    "3": "abc",
    "4": 123,
    "5": "123",
    "6": "",
    "7": "abc"
} 

空手道回应:

{
    "colDefs": [{
        "entityAttributeId": "abc",
        "headerName": "xyz",
        "field": "2",
        "entityPath": "",
        "entityId": "mna"
    }, {
        "entityAttributeId": "abc",
        "headerName": "abc",
        "field": "3",
        "entityPath": "abc",
        "entityId": "abc"
}

推荐答案

很明显,您的响应不是有效的JSON,并且采用某些专有格式-并且期望空手道为您神奇地转换它,因此期望值很高:)

Clearly your response is not valid JSON and in some proprietary format - and expecting Karate to magically convert it for you is expecting too much :)

我建议您编写一些Java代码来进行自定义验证.这里发生的是Karate尽力而为-能够将一半的响应解析为JSON并放弃其余部分.您实际上应该对此表示赞赏:)

I suggest you write some Java code to do your custom validation. What is happening here is Karate tries its best - is able to get half the response parsed as JSON and gives up on the rest. You should actually appreciate this :)

由于空手道还以字节为单位保留了原始响应的副本(从0.9.0版开始),因此您可以执行以下操作:

Since Karate also keeps a copy of the raw response as bytes (version 0.9.0 onwards), you can do this:

* string temp = responseBytes

现在,请使用一些Java代码或自定义字符串解析-并根据您对 temp 的要求执行任何操作,这将是Java String .我不得不说,这看起来像是一个设计不良的API.

Now please use some Java code or custom string parsing - and do whatever your requirement demands on temp which will be a Java String. I have to say that this looks like a VERY badly designed API.

这篇关于空手道API测试-我们如何从Content-Type:text/html类型的响应中提取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆