https://github.com/gmazzo/okhttp-client-mock
A simple OKHttp client mock, using a programmable request interceptor
https://github.com/gmazzo/okhttp-client-mock
Last synced: 11 days ago
JSON representation
A simple OKHttp client mock, using a programmable request interceptor
- Host: GitHub
- URL: https://github.com/gmazzo/okhttp-client-mock
- Owner: gmazzo
- License: mit
- Created: 2017-08-07T14:25:07.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2024-10-21T22:57:01.000Z (6 months ago)
- Last Synced: 2024-10-22T19:20:46.551Z (6 months ago)
- Language: Java
- Size: 475 KB
- Stars: 122
- Watchers: 7
- Forks: 21
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-java - OKHttp Client Mock
README
# okhttp-client-mock
A simple OKHttp client mock, using a programmable request interceptor

[](https://central.sonatype.com/artifact/com.github.gmazzo.okhttp.mock/mock-client)
[](https://github.com/gmazzo/okhttp-client-mock/actions/workflows/build.yaml)
[](https://codecov.io/gh/gmazzo/okhttp-client-mock)
[](https://sourcegraph.com/search?q=content:okhttp-mock\b+content:com.github.gmazzo.okhttp+-repo:github.com/gmazzo/okhttp-client-mock&patternType=regexp)## Import
On your `build.gradle` add:
```groovy
dependencies {
testImplementation 'com.github.gmazzo.okhttp.mock:mock-client:'
}
```## Usage
Create an OkHttp request interceptor and record some rules, for instance:
```kotlin
val interceptor = MockInterceptor().apply {rule(get or post or put, url eq "https://testserver/api/login") {
respond(HTTP_401_UNAUTHORIZED).header("WWW-Authenticate", "Basic")
}rule(url eq "https://testserver/api/json") {
respond("{succeed:true}", MEDIATYPE_JSON)
}rule(url eq "https://testserver/api/json") {
respond(resource("sample.json"), MEDIATYPE_JSON)
}rule(path matches "/aPath/(\\w+)".toRegex(), times = anyTimes) {
respond { body("Path was " + it.url().encodedPath()) }
}rule(delete) {
respond(code = HTTP_405_METHOD_NOT_ALLOWED) {
body("{succeed:false}", MEDIATYPE_JSON)
}
}// throw an exception
rule(get) {
respond { throw IllegalStateException("an IO error") }
}}
```Or in Java:
```java
MockInterceptor interceptor = new MockInterceptor();interceptor.addRule()
.get().or().post().or().put()
.url("https://testserver/api/login")
.respond(HTTP_401_UNAUTHORIZED)
.header("WWW-Authenticate", "Basic");interceptor.addRule()
.get("https://testserver/api/json")
.respond("{succeed:true}", MEDIATYPE_JSON);interceptor.addRule()
.get("https://testserver/api/json")
.respond(resource("sample.json"), MEDIATYPE_JSON);interceptor.addRule()
.pathMatches(Pattern.compile("/aPath/(\\w+)"))
.anyTimes()
.answer(request -> new Response.Builder()
.code(200)
.body(ResponseBody.create(null, "Path was " + request.url().encodedPath())));
```Then add the interceptor to your OkHttpClient client and use it as usual:
```java
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();
```Check an example [Integration Test](/library/src/test/java/okhttp3/mock/MockInterceptorITTest.java) with mocked HTTP
responsesYou can use the following helper classes to provide mock responses from resources:
- `ClasspathResources.resource` to load content from classpath
- `AndroidResources.asset` to load content from an Android's asset
- `AndroidResources.rawRes` to load content from an Android's raw resource
- `RoboResources.asset` and `RoboResources.rawRes` if you are
running [Roboelectric](https://github.com/robolectric/robolectric) tests