Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(java): ✨ added form data body support #410

Merged
merged 2 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<surefire-version>3.0.0-M9</surefire-version>
<nexus.version>1.6.13</nexus.version>
<maven.gpg.version>3.0.1</maven.gpg.version>
<maven-jar-plugin.version>3.3.0</maven-jar-plugin.version>
</properties>

<distributionManagement>
Expand Down Expand Up @@ -343,6 +344,11 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static com.github.wasiqb.boyka.utils.ErrorHandler.handleAndThrow;
import static com.github.wasiqb.boyka.utils.SettingUtils.loadSetting;
import static com.github.wasiqb.boyka.utils.StringUtils.interpolate;
import static java.lang.String.join;
import static java.text.MessageFormat.format;
import static java.time.Duration.ofSeconds;
import static java.util.Objects.requireNonNull;
Expand All @@ -38,6 +39,7 @@
import static org.apache.logging.log4j.LogManager.getLogger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -118,6 +120,7 @@ public ApiResponse execute () {
.basicAuth (this.apiRequest.getUserName (), this.apiRequest.getPassword ())
.body (requireNonNullElse (this.apiRequest.getBody (), EMPTY))
.body (this.apiRequest.getBodyObject ())
.body (this.apiRequest.getFormBodies ())
.method (this.apiRequest.getMethod ())
.getResponse (this.apiRequest.getQueryParams (), this.apiRequest.getPath ()));
}
Expand Down Expand Up @@ -153,6 +156,17 @@ private ApiActions body (final String body) {
return LOGGER.traceExit (this);
}

private ApiActions body (final Map<String, String> bodyMap) {
LOGGER.traceEntry ();
final var body = new ArrayList<String> ();
bodyMap.forEach ((k, v) -> body.add (format ("{0}={1}", k, v)));
if (!body.isEmpty ()) {
this.requestBody = create (join ("&", body),
requireNonNull (this.mediaType, CONTENT_TYPE_NOT_SET.getMessageText ()));
}
return LOGGER.traceExit (this);
}

private ApiActions contentType (final ContentType contentType) {
LOGGER.traceEntry ("Parameter : {}", contentType);
this.mediaType = parse (requireNonNullElse (contentType, JSON).getType ());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class ApiRequest {
private String configKey;
private ContentType contentType;
@Singular
private Map<String, String> formBodies;
@Singular
private Map<String, String> headers;
private RequestMethod method;
private String password;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* MIT License
*
* Copyright (c) 2023, Wasiq Bhamla
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/

package com.github.wasiqb.boyka.testng.api.postman;

import com.github.wasiqb.boyka.actions.api.ApiActions;
import com.github.wasiqb.boyka.builders.ApiRequest;
import com.github.wasiqb.boyka.enums.ContentType;
import com.github.wasiqb.boyka.enums.RequestMethod;
import org.testng.annotations.Test;

/**
* Test class to test Postman related samples.
*
* @author Wasiq Bhamla
* @since 09-Mar-2023
*/
public class ApiPostmanTest {
/**
* Test form data request body related API request.
*/
@Test
public void testFormBodyRequest () {
final var request = ApiRequest.createRequest ()
.configKey ("test_postman")
.contentType (ContentType.FORM_URLENCODED)
.formBody ("strange", "boom")
.formBody ("test", "abc")
.method (RequestMethod.POST)
.path ("/post")
.create ();
final var response = ApiActions.withRequest (request)
.execute ();
response.verifyStatusCode ()
.isEqualTo (200);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022 Wasiq Bhamla
* Copyright (c) 2023, Wasiq Bhamla
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -14,13 +14,13 @@
* copies or substantial portions of the Software.
*/

package com.github.wasiqb.boyka.testng.api;
package com.github.wasiqb.boyka.testng.api.restful;

import static com.github.wasiqb.boyka.actions.api.ApiActions.withRequest;

import com.github.wasiqb.boyka.testng.api.requests.BookingData;
import com.github.wasiqb.boyka.testng.api.requests.BookingDataBuilder;
import com.github.wasiqb.boyka.testng.api.requests.BookingRequest;
import com.github.wasiqb.boyka.testng.api.restful.requests.BookingData;
import com.github.wasiqb.boyka.testng.api.restful.requests.BookingDataBuilder;
import com.github.wasiqb.boyka.testng.api.restful.requests.BookingRequest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022 Wasiq Bhamla
* Copyright (c) 2023, Wasiq Bhamla
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -14,7 +14,7 @@
* copies or substantial portions of the Software.
*/

package com.github.wasiqb.boyka.testng.api.requests;
package com.github.wasiqb.boyka.testng.api.restful.requests;

import lombok.Builder;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* copies or substantial portions of the Software.
*/

package com.github.wasiqb.boyka.testng.api.requests;
package com.github.wasiqb.boyka.testng.api.restful.requests;

import java.text.SimpleDateFormat;
import java.util.concurrent.TimeUnit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* copies or substantial portions of the Software.
*/

package com.github.wasiqb.boyka.testng.api.requests;
package com.github.wasiqb.boyka.testng.api.restful.requests;

import lombok.Builder;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* copies or substantial portions of the Software.
*/

package com.github.wasiqb.boyka.testng.api.requests;
package com.github.wasiqb.boyka.testng.api.restful.requests;

import static com.github.wasiqb.boyka.actions.api.ApiActions.withRequest;
import static com.github.wasiqb.boyka.builders.ApiRequest.createRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* copies or substantial portions of the Software.
*/

package com.github.wasiqb.boyka.testng.api.requests;
package com.github.wasiqb.boyka.testng.api.restful.requests;

import lombok.Builder;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* copies or substantial portions of the Software.
*/

package com.github.wasiqb.boyka.testng.api.requests;
package com.github.wasiqb.boyka.testng.api.restful.requests;

/**
* Token builder class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* copies or substantial portions of the Software.
*/

package com.github.wasiqb.boyka.testng.api.requests;
package com.github.wasiqb.boyka.testng.api.restful.requests;

import lombok.Builder;
import lombok.Getter;
Expand Down
11 changes: 11 additions & 0 deletions core-java/src/test/resources/boyka-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,17 @@
"request": true,
"response": true
}
},
"test_postman": {
"base_uri": "https://postman-echo.com",
"read_timeout": 2,
"write_timeout": 2,
"connection_timeout": 1,
"schema_path": "schema/",
"logging": {
"request": true,
"response": true
}
}
}
}
7 changes: 6 additions & 1 deletion core-java/test-suites/testng-api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<suite name="Boyka API Suite" verbose="2">
<test name="Test Boyka API">
<classes>
<class name="com.github.wasiqb.boyka.testng.api.RestfulBookerEndToEndTests">
<class name="com.github.wasiqb.boyka.testng.api.restful.RestfulBookerEndToEndTests">
<methods>
<include name="testCreateBooking"/>
<include name="testGetBooking"/>
Expand All @@ -29,6 +29,11 @@
<include name="testDeletedBooking"/>
</methods>
</class>
<class name="com.github.wasiqb.boyka.testng.api.postman.ApiPostmanTest">
<methods>
<include name="testFormBodyRequest"/>
</methods>
</class>
</classes>
</test>
</suite>
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@
"devDependencies": {
"@commitlint/cli": "^17.4.4",
"@commitlint/config-conventional": "^17.4.4",
"@types/node": "^18.14.5",
"@typescript-eslint/eslint-plugin": "^5.54.0",
"@typescript-eslint/parser": "^5.54.0",
"@types/node": "^18.14.6",
"@typescript-eslint/eslint-plugin": "^5.54.1",
"@typescript-eslint/parser": "^5.54.1",
"commitlint": "^17.4.4",
"eslint": "^8.35.0",
"eslint-config-google": "^0.14.0",
"eslint-config-prettier": "^8.6.0",
"eslint-config-prettier": "^8.7.0",
"eslint-import-resolver-typescript": "^3.5.3",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.32.2",
"husky": "^8.0.3",
"lerna": "^6.5.1",
"lerna-changelog": "^2.2.0",
"lint-staged": "^13.1.2",
"nx": "^15.8.3",
"lint-staged": "^13.1.4",
"nx": "^15.8.5",
"prettier": "^2.8.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
15 changes: 15 additions & 0 deletions website/docs/api/builders/api-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,21 @@ final ApiRequest request = ApiRequest.configKey (API_CONFIG_KEY)
.create ();
```

## `formBody`

This method is used to set the Form body of the request using key and value pairs.

```java
final ApiRequest request = ApiRequest.configKey (API_CONFIG_KEY)
.method (POST)
.path ("/users")
// highlight-start
.formBody ("strange", "boom")
.formBody ("test", "abc")
// highlight-end
.create ();
```

## `method`

This method is used to set the method of the request. Following are the allowed values:
Expand Down
4 changes: 4 additions & 0 deletions website/docs/contributing/machine-setup/build-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ The core framework project is located in `core-java/` directory.

`Maven` is the build tool we would be using for this project. So need to have Maven installed on your machine before you proceed to build the project.

:::danger Minimum Maven version check
Minimum version for Maven `3.8.0` is required, else, project will **not build**
:::

In case Maven is not installed on your machine, the following steps should help.

#### Installing Maven on Windows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ title: 🚩 Pre-Requisite
In order to use this framework, you need to have the following pre-requisites installed on your machine:

- [Java 11][jdk11]
- [Maven][maven] OR [Gradle][gradle]
- [Maven 3.8 or above][maven] OR [Gradle][gradle]

[jdk11]: https://www.openlogic.com/openjdk-downloads?field_java_parent_version_target_id=406&field_operating_system_target_id=All&field_architecture_target_id=All&field_java_package_target_id=396
[maven]: https://maven.apache.org/install.html
Expand Down
Loading