Skip to content
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
101 changes: 12 additions & 89 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
<h1>HTTP Mock Lib</h1>

<a href="https://beyondthecloud.dev"><img alt="Beyond The Cloud logo" src="https://img.shields.io/badge/MADE_BY_BEYOND_THE_CLOUD-555?style=for-the-badge"></a>
<a ><img alt="API version" src="https://img.shields.io/badge/api-v64.0-blue?style=for-the-badge"></a>
<a ><img alt="API version" src="https://img.shields.io/badge/api-v65.0-blue?style=for-the-badge"></a>
<a href="https://github.com/beyond-the-cloud-dev/http-mock-lib/blob/main/LICENSE"><img alt="License" src="https://img.shields.io/badge/license-mit-green?style=for-the-badge"></a>

[![CI](https://github.com/beyond-the-cloud-dev/http-mock-lib/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/beyond-the-cloud-dev/http-mock-lib/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/beyond-the-cloud-dev/http-mock-lib/branch/main/graph/badge.svg)](https://codecov.io/gh/beyond-the-cloud-dev/http-mock-lib)
</div>

# Getting Started

HTTP Mock inspired by Robert Sösemann’s [Apex Http Mock](https://github.com/rsoesemann/apex-httpmock).

HTTP Mock Lib is part of [Apex Fluently](https://apexfluently.beyondthecloud.dev/), a suite of production-ready Salesforce libraries by Beyond the Cloud.
Expand Down Expand Up @@ -113,95 +114,17 @@ public interface HttpMockLib {
}
```

## Features

### Mock different HTTP methods

Mock different HTTP methods in the same test method.

```java
new HttpMock()
.whenGetOn('/api/v1/authorize').body('{ "token": "aZ3Xb7Qk" }').statusCodeOk()
.whenPostOn('/api/v1/create').body('{ "success": true, "message": null }').statusCodeOk()
.mock();
```

Supported methods:
- GET
- POST
- PUT
- PATCH
- DELETE
- TRACE
- HEAD
## Documentation

### Return different body
Visit the [documentation](https://httpmock.beyondthecloud.dev/) to view the full documentation.

```java
new HttpMock()
.whenGetOn('/api/v1').body(new Map<String, String>{ 'token' => 'aZ3Xb7Qk' }).statusCodeOk()
.mock();
```

Mock HTTP response body by using the following methods:
## Contributors

```java
HttpMock body(Object body);
HttpMock body(String body);
HttpMock body(Blob body);
```
<a href="https://github.com/beyond-the-cloud-dev/http-mock-lib/graphs/contributors">
<img src="https://contrib.rocks/image?repo=beyond-the-cloud-dev/http-mock-lib" />
</a>

### Use built-in Content Types
## License notes:

Use different content types. By default, the content type is set to `application/json`.

```java
HttpMock contentTypePlainText(); // text/plain
HttpMock contentTypeHtml(); // text/html
HttpMock contentTypeCsv(); // text/csv
HttpMock contentTypeJson(); // application/json
HttpMock contentTypeXml(); // application/xml
HttpMock contentTypePdf(); // application/pdf
HttpMock contentTypeFormUrlencoded(); // application/x-www-form-urlencoded

HttpMock contentType(String contentType);
```

Use `contentType(String contentType)` to set your own content type.

### Use built-in Status Codes

Use different status codes. By default, the status code is set to 200 (OK).

Available status codes:

```java
HttpMock statusCodeOk(); // 200
HttpMock statusCodeCreated(); // 201
HttpMock statusCodeAccepted(); // 202
HttpMock statusCodeNoContent(); // 204
HttpMock statusCodeBadRequest(); // 400
HttpMock statusCodeUnauthorized(); // 401
HttpMock statusCodeForbidden(); // 403
HttpMock statusCodeNotFound(); // 404
HttpMock statusCodeMethodNotAllowed(); // 405
HttpMock statusCodeInternalServerError(); // 500
HttpMock statusCodeNotImplemented(); // 501
HttpMock statusCodeBadGateway(); // 502
HttpMock statusCodeServiceUnavailable(); // 503
HttpMock statusCodeGatewayTimeout(); // 504

HttpMock statusCode(Integer statusCode);
```

Use `statusCode(Integer statusCode)` to set your own status code.

### Set custom headers

Set response headers using the `header(String key, String value)` method.

```java
new HttpMock()
.whenGetOn('/api/v1').body('{ "token": "aZ3Xb7Qk" }').header('Cache-Control', 'no-cache')
.mock();
```
- For proper license management each repository should contain LICENSE file similar to this one.
- each original class should contain copyright mark: © Copyright 2025, Beyond The Cloud Sp. z o.o. (BeyondTheCloud.Dev)
Loading