如何使用空手道框架将字节数组作为Json的一部分发送 [英] How to send a byte array as a part of Json with karate framework

查看:48
本文介绍了如何使用空手道框架将字节数组作为Json的一部分发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Json的端点,该端点具有2个属性,例如

I have an endpoint who consumes Json with 2 attributes, like

{id='12344', data=byte_array} 

所以我写了一个测试

Feature: submitted request

Scenario: submitted request
* def convertToBytes =
"""
function(arg) {
    var StreamUtils = Java.type('my.utils.StreamUtils');
    // it reads stream and convert it to a byte array
    return StreamUtils.getBytes(arg);
}
"""

 Given url 'http://my-server/post'
 And def image = convertToBytes(read('classpath:images/image_1.jpg'));
 And request {id:1, data: "#(image)"}
 When method POST
 Then status 200

但是空手道异常表却没有太多细节

However is got an exception form karate without much details

ERROR com.intuit.karate - http request failed: [B cannot be cast to [Ljava.lang.Object;

有没有人想知道如何使用空手道将字节数组作为Json的一部分提交?

Any hits how to submit byte arrays as a part of Json with karate?

推荐答案

我认为您无法做到这一点. 整个请求应为二进制(字节数组),或者您执行多部分请求,其中二进制文件是Base64编码的.据我所知,您不能将二进制文件放入JSON.不过,有些东西叫做二进制JSON .

I don't think you can do that. Either the whole request should be binary (byte-array) or you do a multi-part request, where binary is Base64 encoded. As far as I know you can't put binary inside JSON. There is something called Binary JSON though.

假定byte []必须是Base64编码后:

after assuming that the byte[] has to be Base64 encoded:

Background:
    * url demoBaseUrl
    * def Base64 = Java.type('java.util.Base64')

Scenario: json with byte-array
    Given path 'echo', 'binary'
    And def encoded = Base64.encoder.encodeToString('hello'.bytes);
    And request { message: 'hello', data: '#(encoded)' }
    When method post
    Then status 200
    And def expected = Base64.encoder.encodeToString('world'.bytes);
    And match response == { message: 'world', data: '#(expected)' }

我刚刚将此测试添加到了空手道演示中,并且工作正常.这是提交.

I just added this test to the Karate demos, and it is working fine. Here is the commit.

这篇关于如何使用空手道框架将字节数组作为Json的一部分发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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