通过python的requests给接口发送请求进行测试
#coding:utf-8
import requests
class TestApi():
url_login = "https://legend-sit.omodaglobal.com/api/auth/oauth2/token"
url_topic_b = "https://legend-sit.omodaglobal.com/api/community/topic_b/page?size=10¤t=1"
def __init__(self):
pass
def test_log(self):
header = {}
header["Content-Type"] = "application/x-www-form-urlencoded"
header["Tenant-Id"] = "1"
header["Authorization"] = "Basic bGVnZW5kV2ViOmxlZ2VuZFdlYg=="
body = {}
body["username"] = "majianxiong"
body["randomStr"] = "blockPuzzle"
body["grant_type"] = "password"
body["scope"] = "server"
body["password"] = "VN7bZl87c2zhlQhSuLVvhw=="
response = requests.post(self.url_login,headers=header,data=body)
print(response.json())
token_type = response.json()["data"]["token_type"]
access_token = response.json()["data"]["access_token"]
print(token_type,access_token)
return token_type,access_token
def test_topic_b(self):
token_type,access_token = self.test_log()
header = {}
header["Tenant-Id"] = "1"
header["Authorization"] = token_type + " " + access_token
response = requests.get(self.url_topic_b,headers=header)
print(response.json())
testapi = TestApi()
testapi.test_topic_b()
通过java的HttpClients给接口发送请求进行测试
package org.example;import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; 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.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils;import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import org.json.JSONObject; /*** Hello world!**/class Test{String test_log(){CloseableHttpClient httpClient = HttpClients.createDefault();String url_login = "https://legend-sit.omodaglobal.com/api/auth/oauth2/token";//创建httppost对象HttpPost httpPost = new HttpPost(url_login);httpPost.setHeader("Content-Type","application/x-www-form-urlencoded");httpPost.setHeader("Tenant-Id","1");httpPost.setHeader("Authorization","Basic bGVnZW5kV2ViOmxlZ2VuZFdlYg==");List<NameValuePair> parameters = new ArrayList<>();parameters.add(new BasicNameValuePair("username","majianxiong"));parameters.add(new BasicNameValuePair("randomStr","blockPuzzle"));parameters.add(new BasicNameValuePair("grant_type","password"));parameters.add(new BasicNameValuePair("scope","server"));parameters.add(new BasicNameValuePair("password","VN7bZl87c2zhlQhSuLVvhw=="));try {httpPost.setEntity(new UrlEncodedFormEntity(parameters));} catch (UnsupportedEncodingException e) {throw new RuntimeException(e);}try {CloseableHttpResponse response = httpClient.execute(httpPost);HttpEntity entity = response.getEntity();String responseText = EntityUtils.toString(entity, StandardCharsets.UTF_8);org.json.JSONObject jsonObject = new org.json.JSONObject(responseText);org.json.JSONObject jsonObjectData = jsonObject.getJSONObject("data");String token_type = jsonObjectData.getString("token_type");String access_token = jsonObjectData.getString("access_token");System.out.println(token_type+" "+access_token);return token_type+" "+access_token;} catch (Exception e) {throw new RuntimeException(e);}}void test_topic_b(){String token = test_log();CloseableHttpClient httpClient = HttpClients.createDefault();String url_topic_b = "https://legend-sit.omodaglobal.com/api/community/topic_b/page?size=10¤t=1";//构造httpGet请求对象HttpGet httpGet = new HttpGet(url_topic_b);httpGet.setHeader("Tenant-Id","1");httpGet.setHeader("Authorization",token);try {CloseableHttpResponse response = httpClient.execute(httpGet);HttpEntity entity = response.getEntity();String responseText = EntityUtils.toString(entity, StandardCharsets.UTF_8);System.out.println(responseText);} catch (IOException e) {throw new RuntimeException(e);}}} public class TestAPI{public static void main(String[] args) {Test test = new Test();test.test_topic_b();} }
工具发送接口测试
jmeter
通过jmeter发送接口请求进行测试
postman
脚本
数据库连接工具DBeaver
推荐的想法
本人比较推荐如果代码用python(书写比较简单),工具用jmeter。postman的功能有缺失比如连接数据库或者非http协议的支持没有jmeter友好。但jmeter学习起来要比postman复杂点。为了功能的完善推荐jmeter