-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
191 additions
and
6 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...r/Bai3_SendRequest_GET/DemoAddHeader.java → ..._SendRequest_GETmethod/DemoAddHeader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...er/Bai3_SendRequest_GET/DemoAddParam.java → ...3_SendRequest_GETmethod/DemoAddParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...i3_SendRequest_GET/DemoGivenWhenThen.java → ...dRequest_GETmethod/DemoGivenWhenThen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/test/java/com/anhtester/Bai7_Authentication_PUTmethod/BasicAuth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.anhtester.Bai7_Authentication_PUTmethod; | ||
|
||
import io.restassured.response.Response; | ||
import io.restassured.specification.RequestSpecification; | ||
import org.testng.annotations.Test; | ||
|
||
import static io.restassured.RestAssured.given; | ||
|
||
public class BasicAuth { | ||
@Test | ||
public void getData() { | ||
RequestSpecification httpRequest = given(); | ||
Response response = httpRequest.get("https://postman-echo.com/basic-auth"); | ||
|
||
System.out.println("Data from the GET API: "); | ||
System.out.println(response.getStatusCode()); | ||
System.out.println(response.getBody().asString()); | ||
|
||
response.then().statusCode(200); | ||
} | ||
|
||
@Test | ||
public void testBasicAuth() { | ||
RequestSpecification httpRequest = given().auth().basic("postman", "password"); | ||
|
||
Response response = httpRequest.get("https://postman-echo.com/basic-auth"); | ||
|
||
System.out.println("Data from the GET API: "); | ||
System.out.println(response.getStatusCode()); | ||
System.out.println(response.getBody().asString()); | ||
|
||
response.then().statusCode(200); | ||
} | ||
|
||
@Test | ||
public void testPreemptiveBasicAuth() { | ||
RequestSpecification httpRequest = given().auth().preemptive().basic("postman", "password"); | ||
|
||
Response response = httpRequest.get("https://postman-echo.com/basic-auth"); | ||
|
||
System.out.println("Data from the GET API: "); | ||
System.out.println(response.getStatusCode()); | ||
System.out.println(response.getBody().asString()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/test/java/com/anhtester/Bai7_Authentication_PUTmethod/DigestAuth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.anhtester.Bai7_Authentication_PUTmethod; | ||
|
||
import io.restassured.RestAssured; | ||
import io.restassured.response.Response; | ||
import io.restassured.specification.RequestSpecification; | ||
import org.testng.annotations.Test; | ||
|
||
import static io.restassured.RestAssured.given; | ||
|
||
public class DigestAuth { | ||
@Test | ||
public void testDigestAuth() { | ||
RequestSpecification httpRequest = given().auth().digest("postman", "password"); | ||
|
||
Response response = httpRequest.get("https://postman-echo.com/basic-auth"); | ||
|
||
System.out.println("Data from the GET API: "); | ||
System.out.println(response.getStatusCode()); | ||
System.out.println(response.getBody().asString()); | ||
|
||
response.then().statusCode(200); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/java/com/anhtester/Bai7_Authentication_PUTmethod/FormAuth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.anhtester.Bai7_Authentication_PUTmethod; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import static io.restassured.RestAssured.given; | ||
|
||
public class FormAuth { | ||
|
||
@Test | ||
public void testFormAuth() { | ||
given() | ||
.auth() | ||
.form("value1", "value2") | ||
.get("your end point URL"); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/test/java/com/anhtester/Bai7_Authentication_PUTmethod/Oauth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.anhtester.Bai7_Authentication_PUTmethod; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import static io.restassured.RestAssured.given; | ||
|
||
public class Oauth { | ||
@Test | ||
public void testOAuth1() { | ||
given() | ||
.auth() | ||
.oauth("consumerKey", "consumerSecret", "accessToken", "tokenSecret") | ||
.get("your end point URL"); | ||
|
||
} | ||
|
||
@Test | ||
public void testOAuth2() { | ||
given() | ||
.auth() | ||
.oauth2("Access token") | ||
.get("your end point URL"); | ||
|
||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/test/java/com/anhtester/Bai7_Authentication_PUTmethod/PUTmethod_TOKEN_COOKIE.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.anhtester.Bai7_Authentication_PUTmethod; | ||
|
||
import com.anhtester.model.LoginPOJO; | ||
import com.anhtester.model.RegisterUserPOJO; | ||
import com.google.gson.Gson; | ||
import io.restassured.response.Response; | ||
import io.restassured.specification.RequestSpecification; | ||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import static io.restassured.RestAssured.given; | ||
|
||
public class PUTmethod_TOKEN_COOKIE { | ||
|
||
//Khai báo biến toàn tục TOKEN để lưu trữ từ hàm Login | ||
String TOKEN = ""; | ||
|
||
@BeforeMethod | ||
public void loginUser() { | ||
|
||
//Khởi tạo giá trị cho các fields thông qua hàm xây dựng | ||
LoginPOJO loginPOJO = new LoginPOJO("anhtester", "Demo@123"); | ||
|
||
//Dùng thư viện Gson để chuyển class POJO về dạng JSON | ||
Gson gson = new Gson(); | ||
|
||
RequestSpecification request = given(); | ||
request.baseUri("https://api.anhtester.com/api") | ||
.accept("application/json") | ||
.contentType("application/json") | ||
.body(gson.toJson(loginPOJO)); | ||
|
||
Response response = request.when().post("/login"); | ||
response.prettyPrint(); | ||
|
||
response.then().statusCode(200); | ||
|
||
//Lưu giá trị token vào biến TOKEN nhé | ||
TOKEN = response.getBody().path("token"); | ||
System.out.println(TOKEN); | ||
} | ||
|
||
@Test | ||
public void testEditUser_NoAuth() { | ||
|
||
//Chuẩn bị data | ||
RegisterUserPOJO registerUserPOJO = new RegisterUserPOJO(); | ||
registerUserPOJO.setUsername("myduyen5"); | ||
registerUserPOJO.setPassword("Demo@123"); | ||
registerUserPOJO.setFirstName("Lê Thị"); | ||
registerUserPOJO.setLastName("Mỹ Duyên"); | ||
registerUserPOJO.setEmail("[email protected]"); | ||
registerUserPOJO.setPhone("0123456789"); | ||
registerUserPOJO.setUserStatus(1); | ||
|
||
Gson gson = new Gson(); | ||
|
||
RequestSpecification request = given(); | ||
request.baseUri("https://api.anhtester.com/api") | ||
.accept("application/json") | ||
.contentType("application/json") | ||
.header("Authorization", "Bearer " + TOKEN) | ||
.body(gson.toJson(registerUserPOJO)); | ||
|
||
Response response = request.when().put("/user/7"); | ||
|
||
System.out.println(response.getStatusCode()); | ||
response.prettyPrint(); | ||
|
||
response.then().statusCode(200); | ||
|
||
String message = response.getBody().path("message"); | ||
Assert.assertEquals(message, "Success", "The message not match."); | ||
} | ||
|
||
} |