接口测试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>
相关推荐
前网易架构师-高司机10 天前
带标注的驾驶员安全带识别数据集,识别率99.5%,可识别有无系安全带,支持yolo,coco json,pascal voc xml格式
xml·yolo·数据集·交通·安全带
逍遥德10 天前
Maven教程.01- settings.xml 文件<profile>使用详解
xml·java·maven
逍遥德10 天前
Maven教程.03-如何阅读pom.xml文件
xml·java·后端·maven
上海合宙LuatOS10 天前
LuatOS核心库API——【json 】json 生成和解析库
java·前端·网络·单片机·嵌入式硬件·物联网·json
敲代码的柯基10 天前
一篇文章理解tsconfig.json和vue.config.js
javascript·vue.js·json
万物得其道者成11 天前
前端大整数精度丢失:一次踩坑后的实战解决方案(`json-bigint`)
前端·json
松叶似针11 天前
Flutter三方库适配OpenHarmony【doc_text】— .docx 解析全流程:从 ZIP 解压到 XML 提取
xml·flutter·harmonyos
Ai runner11 天前
Show call stack in perfetto from json input
java·前端·json
ID_1800790547311 天前
淘宝商品详情API请求的全场景,带json数据参考
服务器·数据库·json
松叶似针11 天前
Flutter三方库适配OpenHarmony【doc_text】— parseDocxXml:正则驱动的 XML 文本提取
xml·flutter