接口测试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>
相关推荐
itachi-uchiha2 天前
awk处理xml文件&&封装集合变量和调用
xml·shell·awk
远方16092 天前
16-Oracle 23 ai-JSON-Relational Duality-知识准备
数据库·oracle·json
sc写算法2 天前
基于nlohmann/json 实现 从C++对象转换成JSON数据格式
开发语言·c++·json
酷爱码3 天前
Spring Boot项目中JSON解析库的深度解析与应用实践
spring boot·后端·json
不惑_3 天前
用 PyQt5 打造一个可视化 JSON 数据解析工具
开发语言·qt·json
漫游者Nova3 天前
PDF转Markdown/JSON软件MinerU最新1.3.12版整合包下载
pdf·json·markdown·mineru
snow@li3 天前
vue3+ts+vite:详细、完整的 tsconfig.json 示例 / 常见配置项及其用途
json·tsconfig.json
南郁4 天前
007-nlohmann/json 项目应用-C++开源库108杰
c++·开源·json·nlohmann·现代c++·d2school·108杰
紫乾20144 天前
idea json生成实体类
java·json·intellij-idea
武子康4 天前
Java-39 深入浅出 Spring - AOP切面增强 核心概念 通知类型 XML+注解方式 附代码
xml·java·大数据·开发语言·后端·spring