java 直线_基于java的直线型接口测试框架初探

在使用java语言作为接口测试的过程中,发现java语言的简洁性的确不如脚本语言,如python,很多功能python一行代码几个方法就能搞定,java需要几行,而且有时候并不利于理解。最近接触到了一个词“直线型”代码。看了之后有所感觉,重新写了一个直线型代码风格的接口请求框架。

源码如下:

package com.fun.frame.httpclient

import com.fun.base.bean.RequestInfo

import com.fun.config.RequestType

import net.sf.json.JSONObject

import org.apache.commons.lang3.StringUtils

import org.apache.http.Header

import org.apache.http.client.methods.HttpRequestBase

import org.slf4j.Logger

import org.slf4j.LoggerFactory

/**

* 重写FanLibrary,使用面对对象思想

*/

public class FunRequest extends FanLibrary {

static Logger logger = LoggerFactory.getLogger(FunRequest.class)

/**

* 请求类型,true为get,false为post

*/

RequestType requestType

/**

* 请求对象

*/

HttpRequestBase request

/**

* host地址

*/

String host

/**

* 接口地址

*/

String apiName

/**

* 请求地址,如果为空则由host和apiname拼接

*/

String uri

/**

* header集合

*/

List headers = new ArrayList<>()

/**

* get参数

*/

JSONObject args = new JSONObject()

/**

* post参数

*/

JSONObject params = new JSONObject()

/**

* json参数

*/

JSONObject json = new JSONObject()

/**

* 构造方法

*

* @param requestType

*/

private FunRequest(RequestType requestType) {

this.requestType = requestType

}

/**

* 获取get对象

*

* @return

*/

public static FunRequest isGet() {

new FunRequest(RequestType.GET)

}

/**

* 获取post对象

*

* @return

*/

public static FunRequest isPost() {

new FunRequest(RequestType.POST)

}

/**

* 设置host

*

* @param host

* @return

*/

public FunRequest setHost(String host) {

this.host = host

this

}

/**

* 设置接口地址

*

* @param apiName

* @return

*/

public FunRequest setApiName(String apiName) {

this.apiName = apiName

this

}

/**

* 设置uri

*

* @param uri

* @return

*/

public FunRequest setUri(String uri) {

this.uri = uri

this

}

/**

* 添加get参数

*

* @param key

* @param value

* @return

*/

public FunRequest addArgs(Object key, Object value) {

args.put(key, value)

this

}

/**

* 添加post参数

*

* @param key

* @param value

* @return

*/

public FunRequest addParam(Object key, Object value) {

params.put(key, value)

this

}

/**

* 添加json参数

*

* @param key

* @param value

* @return

*/

public FunRequest addJson(Object key, Object value) {

json.put(key, value)

this

}

/**

* 添加header

*

* @param key

* @param value

* @return

*/

public FunRequest addHeader(Object key, Object value) {

headers << getHeader(key.toString(), value.toString())

this

}

/**

* 添加header

*

* @param header

* @return

*/

public FunRequest addHeader(Header header) {

headers.add(header)

this

}

/**

* 批量添加header

*

* @param header

* @return

*/

public FunRequest addHeader(List header) {

header.each { h -> headers << h }

this

}

/**

* 增加header中cookies

*

* @param cookies

* @return

*/

public FunRequest addCookies(JSONObject cookies) {

headers << getCookies(cookies)

this

}

/**

* 获取请求响应,兼容相关参数方法,不包括file

*

* @return

*/

public JSONObject getResponse() {

if (StringUtils.isEmpty(uri))

uri = host + apiName

switch (requestType) {

case RequestType.GET:

request = FanLibrary.getHttpGet(uri, args)

break

case RequestType.POST:

request = !params.isEmpty() ? FanLibrary.getHttpPost(uri + changeJsonToArguments(args), params) : !json.isEmpty() ? getHttpPost(uri + changeJsonToArguments(args), json.toString()) : getHttpPost(uri + changeJsonToArguments(args))

break

}

headers.each { x -> request.addHeader(x) }

return getHttpResponse(request)

}

/**

* 获取请求对象

*

* @return

*/

public HttpRequestBase getRequest() {

logger.debug("请求信息:{}",new RequestInfo(this.request).toString())

this.request

}

@Override

public String toString() {

JSONObject.fromObject(this).toString()

}

}

使用方法如下:

public static void main(String[] args) {

JSONObject response = FunRequest.isGet()

.setHost("www.funtester.cn")

.setApiName("/test")

.addArgs("uname", "FunTester")

.addArgs("passoword", "FunTester")

.addArgs("type", "FunTester")

.addHeader("token", "FunTester")

.addCookies(getJson("login=false"))

.getResponse();

output(response);

FanLibrary.testOver();

}

技术类文章精选

非技术文章精选

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值