接口运行显示400

来源:9-6 Post方法的访问实战

qq_31画_0

2023-07-05

properties文件:
test.url=http://localhost:8801
getCookies.uri=/getCookie
test.post.cookie=/post/cookie/json

json文件:
[
{
“description”: “Post方法访问”,
“request”: {
“uri”: “/post/cookie/json”,
“method”: “post”,
“cookies”: {
“login”: “true”
},
“json”: {
“name”: “xigua”,
“age”: “18”
}
},
“response”: {
“status”: 200,
“json”: {
“xigua”: “yes”,
“status”: “1”
}
}
}
]

MyCookieForPost类:
package com.muke.testng.cookies;

import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

public class MyCookieForPost {

private String url;  //存储域名
private ResourceBundle bundle;  //获取配置文件内容
private CookieStore store;

@BeforeTest
public void aftertest(){
    bundle=ResourceBundle.getBundle("application", Locale.CHINA);
    this.url=bundle.getString("test.url");
}

@Test
public void testGetcookies() throws IOException {

    String result;

    //从配置文件中拼接测试的URL
    String uri=bundle.getString("getCookies.uri");
    String testUrl=this.url+uri;

    // 获取cookies信息
    this.store= new BasicCookieStore();
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(store).build();

    //测试逻辑
    HttpGet httpget=new HttpGet(testUrl);
    CloseableHttpResponse response = httpclient.execute(httpget);

    //打印返回值
    result = EntityUtils.toString(response.getEntity());
    System.out.println(result);

    //读取cookie信息
    List<Cookie> cookielist = store.getCookies();
    for(Cookie cookie: cookielist){
        String name=cookie.getName();
        String value=cookie.getValue();
        System.out.println("cookie name =" + name);
        System.out.println("Cookie value=" + value);
    }
}


@Test(dependsOnMethods = {"testGetcookies"})
public void testPostMethod() throws IOException {
    String uri=bundle.getString("test.post.cookie");
    //拼接最终的测试地址
    String testUrl=this.url+uri;

    //声明一个post的方法
    HttpPost httppost=new HttpPost(testUrl);

    //添加参数
    JSONObject param = new JSONObject();
    param.put("name","xigua");
    param.put("age","18");

    //设置请求头信息,设置header
    httppost.setHeader("content-type", "application/json");

    //将参数信息添加到方法中
    StringEntity entity = new StringEntity(param.toString(), "utf-8");
    httppost.setEntity(entity);

    //声明一个client对象,用来进行方法的执行,并设置cookies信息
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(this.store).build();

    //执行post的方法并得到响应结果
    CloseableHttpResponse response = httpclient.execute(httppost);

    //就是判断返回结果是否符合预期
    int statusCode = response.getStatusLine().getStatusCode();
    System.out.println("statusCode = "+ statusCode);

    String result=EntityUtils.toString(response.getEntity(),"utf-8");;

    if (statusCode==200){
        System.out.println(result);
    } else {
        System.out.println("访问/post/cookie/json接口失败");
    }

    //将返回的响应结果字符串转化为json对象
    JSONObject resultjson = JSONObject.parseObject(result);

    //获取到结果值
    String success = (String) resultjson.get("xigua");
    System.out.println(success);
    Assert.assertEquals("yes", success);

}

}

运行结果显示400
图片描述

写回答

1回答

qq_31画_0

提问者

2023-07-07

已解决,原来是我运行java  -jar包错了

0
0

Java接口自动化测试实战,搞定理论基础+典型应用场景

打破传统测试用例设计方法,搞懂基于TestNG的接口自动化测试技术

2086 学习 · 920 问题

查看课程