接口测试JSON/XML请求

目录:

  • JSON请求
  • XML请求

JSON简介:

  • 是JavaScript Object Notation的缩写
  • 是一种轻量级的数据交换格式
  • 是理想的接口数据交换语言

JSON请求:

  • 构造JSON请求体
  • JSON字符串
  • HashMap对象+Jackson库

构造JSON请求体:

java 复制代码
package com.ceshiren.jsonpath;

import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;

public class jsonStrTest {

    @Test
    void testJsonStr(){


        //定义请求数据:jsonstr
        String jsonStr = "{\"hello\":\"hogwarts\"}";

        given()
                //指定内容类型
                .contentType("application/json")
                //传入数据对象
                .body(jsonStr)
                .log().headers()
                .log().body()
        .when()
                .post("https://httpbin.ceshiren.com/post")
        .then()
//                .log().all()
                //简单的断言
                .statusCode(200);


    }
}

使用HashMap对象:

java 复制代码
package com.ceshiren.jsonpath;

import org.junit.jupiter.api.Test;

import java.util.HashMap;

import static io.restassured.RestAssured.given;

public class jsonObjTest {


    @Test
    void JsonObjTest(){

        //定义请求数据体
        HashMap<String,String> jsonObj  =new HashMap<String,String>();
        //填充数据
        jsonObj.put("hello", "hogwarts");


        given()
                //请求体类型
                .contentType("application/json")
                .body(jsonObj)
                .log().headers()
                .log().body()
        .when()
                .post("https://httpbin.ceshiren.com/post")
        .then()
                .statusCode(200);
    }

}

XML简介:

  • 是eXtensible Markup Language的缩写
  • 是可扩展标记语言,类似HTML
  • 是用来传输和存储数据
  • 是通过<>标签来描述信息
  • 是W3C的推荐标准

XML是一个完整的标记语言,而JSON不是

XML请求:

  • 构建XML请求体
    • 外部XML文件
    • 字符串

外部XML文件:

IOUtils依赖配置:

java 复制代码
 <dependency>
      <groupId>com.jayway.jsonpath</groupId>
      <artifactId>json-path</artifactId>
      <version>${json-path.version}</version>
    </dependency>

xml实例:

java 复制代码
package com.ceshiren.jsonpath;

import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import static io.restassured.RestAssured.given;

public class xmlTest {


    @Test
    void testxmlTest() throws IOException {
        //定义请求体数据,XML文件
        File file = new File("src/test/resources/add.xml");
        //使用文件输入流,读取信息
        FileInputStream fis = new FileInputStream(file);
        //定义请求体的信息
        String reqBody = IOUtils.toString(fis, "UTF-8");
        given()
                .contentType("text/xml")//设定请求内容媒体类型
                .body(reqBody)//定制请求体数据
                .log().headers()
                .log().body()

        .when()
                .post("http://dneonline.com/calculator.asmx")
        .then()
                .log().all()
                .statusCode(500);//响应断言
    }


}

add.txt文件内容:

java 复制代码
<Envelop xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <Add xmlns="http://tempuri.org/">
            <intA>1</intA>
            <intB>1</intB>
        </Add>
    </Body>
</Envelop>
相关推荐
l1t3 天前
利用美团龙猫用libxml2编写XML转CSV文件C程序
xml·c语言·libxml2·解析器
Martin-Luo4 天前
Vue3 通过json配置生成查询表单
javascript·vue.js·json
星尘库4 天前
后端json数据反序列化枚举类型不匹配的错误
json
BXCQ_xuan4 天前
软件工程实践四:MyBatis-Plus 教程(连接、分页、查询)
spring boot·mysql·json·mybatis
王维志4 天前
LiteDB详解
数据库·后端·mongodb·sqlite·c#·json·database
ID_180079054734 天前
Python采集京东店铺所有商品数据,json数据返回
json
ljh5746491194 天前
mysql 必须在逗号分隔字符串和JSON字段之间二选一,怎么选
数据库·mysql·json
小孔龙5 天前
02.Kotlin Serialization 属性序列化控制
kotlin·json
Cachel wood5 天前
信息检索、推荐系统模型排序质量指标:AP@K和MAP@K
windows·搜索引擎·json·推荐系统·搜索
tebukaopu1485 天前
json文件转excel
json·excel