Interface testing based on Http request

  1. Introduce maven dependencies
<dependency>
		  <groupId>io.rest-assured</groupId>
		  <artifactId>rest-assured</artifactId>
		  <version>3.0.0</version>
		  <scope>test</scope>
	  </dependency>
	  <dependency>
		  <groupId>junit</groupId>
		  <artifactId>junit</artifactId>
		  <version>4.10</version>
		  <scope>test</scope>
	  </dependency>
  1. start web service
  2. Add unit test cases
@Before
	public void setup() {
		RestAssured.baseURI = "https://127.0.0.1:6443/as-api";
		RestAssured.basePath = "/api/v1";
		//单项认证允许所有客户端通过
		//RestAssured.useRelaxedHTTPSValidation();
	}

Rest-Assured can directly verify at the time of GET. The following example:

@Test
	public void test1() {
		given()
				.param("param", "1223434")
				.expect()
				.statusCode(200)
				.body("challenge", equalTo("1223434"))
				.when()
				.get("/test2");
	}

Or this (do you find it particularly flexible? In general, an API test can be completed through a chain call

get("/test2?param=1223434").then().body("challenge", equalTo("1223434"));

post request

@Test
	public void test() {
		Map<String, Object> map = new HashMap<>();
		map.put("mode", 2);
		map.put("id", "23452345234");
		map.put("identity", "11111111");
		map.put("challenge", "s234523452345g");
		map.put("response", "98CF0059D520E39E2016EB3AC70763BB26B665D77CD81378F11E5479E7094ACD");
		map.put("sign", "1232321");

		given()
				.contentType(ContentType.JSON)
				.request()
				.body(JSON.toJSONString(map))
				.post("/test").then().assertThat()
				.body("mode", equalTo("2"),"id",equalTo("23452345234")).time(lessThan(1000L), TimeUnit.SECONDS);

	}

The first body is the requestBody, and the second body is the responseBody, and it can be directly asserted whether the content in the corresponding body is the same as what it predicted. And in special cases, it can also detect whether the request time exceeds our expectations. File Upload

@Test
	public void testFile() {
		Response post = RestAssured.given()
				.given().param("id", "sdfgfsdg").multiPart("file1", new File("d:/pic.png"))
				.post("/apply/upload.do");
		System.out.println(post.asString());
	}

If you are two-way authentication, you only need such a chain call. If you use HttpUrlConention or httpclient, more than one line of programs can solve the problem. Of course, this does not mean that httpclient is not easy to use. It can only be said that Rest-assured is with us Provides better packaging, reduces our chances of making mistakes, and enables me to invest more efficiently in our business.

RestAssured.authentication = RestAssured
				.certificate("clq.truststore",
						"123456","clq.p12", "123456",
						CertificateAuthSettings
						.certAuthSettings()
						.keyStoreType("PKCS12")
						.trustStoreType("jks"));

Here is only a simple list based on get and post and HTTP (S) requests, from which we can see that rest-assured is really simple and easy to use. Among them, he also provides many useful APIs, such as manipulating headers or cookies, or changing the port number during the calling process. These can be searched and used according to the API. For details, please refer to the source code of the test case

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324156707&siteId=291194637