Skip to content

Commit

Permalink
Release 0.2.0 (#4)
Browse files Browse the repository at this point in the history
* Add wrapDispatcher method.

* chore: suggestions with mockserver

* Add request, assertions and allow-mocking modules.

---------

Co-authored-by: Jacek Marchwicki <[email protected]>
  • Loading branch information
nowakweronika and jacek-marchwicki authored Apr 19, 2024
1 parent 9e29b19 commit 475275f
Show file tree
Hide file tree
Showing 21 changed files with 168 additions and 52 deletions.
39 changes: 17 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,18 @@ repositories {
Add the dependency:
```kotlin
dependencies {
androidTestImplementation 'com.github.appunite:MockWebServer-Extensions:0.1.0'
androidTestImplementation 'com.github.appunite.MockWebServer-Extensions:mockwebserver-extensions:0.2.0'
androidTestImplementation 'com.github.appunite.MockWebServer-Extensions:mockwebserver-request:0.2.0'
implementation 'com.github.appunite.MockWebServer-Extensions:mockwebserver-interceptor:0.2.0'
}
```

### Add a Network Security Configuration file
### Add a Network Security Configuration

#### Add `app/src/debug/res/xml/network_security_config.xml` file.

```xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
</domain-config>
</network-security-config>
```

#### Add `network_security_config.xml` to the debug AndroidManifest.

```xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application android:networkSecurityConfig="@xml/network_security_config">
</application>
</manifest>
```kotlin
dependencies {
debugImplementation 'com.github.appunite.MockWebServer-Extensions:mockwebserver-allow-mocking:0.2.0'
}
```

### Add TestInterceptor to your HTTP Client.
Expand All @@ -83,6 +69,15 @@ val retrofit = Retrofit.Builder()
.build()
```

### Optional Assertions using okhttp

```kotlin
dependencies {
androidTestImplementation 'com.github.appunite.MockWebServer-Extensions:mockwebserver-assertions:0.2.0'
}
```


You can check full example in the [app module](https://github.com/appunite/MockWebServer/tree/main/app/src).
And more examples in the [Loudius - Android playground](https://github.com/appunite/Loudius) in the [app-shared-tests module](https://github.com/appunite/Loudius/tree/develop/app-shared-tests/src/main/java/com/appunite/loudius).

Expand Down
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ android {
dependencies {

androidTestImplementation(project(":mockwebserver-extensions"))
androidTestImplementation(project(":mockwebserver-assertions"))
androidTestImplementation(project(":mockwebserver-request"))
implementation(project(":mockwebserver-interceptor"))
debugImplementation(project(":mockwebserver-allow-mocking"))

implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.appunite.mockwebserver_assertions.path
import com.appunite.mockwebserver_assertions.url
import com.appunite.mockwebserverextensions.util.jsonResponse
import com.appunite.mockwebserverextensions.util.path
import com.appunite.mockwebserverextensions.util.url
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Rule
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ plugins {
id("org.jetbrains.kotlin.android") version "1.9.0" apply false
id("org.jetbrains.kotlin.jvm") version "1.9.0" apply false
id("org.jetbrains.kotlin.plugin.serialization") version "1.7.10" apply false
id("com.android.library") version "8.2.2" apply false
}
1 change: 1 addition & 0 deletions mockwebserver-allow-mocking/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
51 changes: 51 additions & 0 deletions mockwebserver-allow-mocking/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("maven-publish")
}

android {
namespace = "com.appunite.mockwebserver_allow_mocking"
compileSdk = 34

defaultConfig {
minSdk = 24
}

buildTypes {
release {
isMinifyEnabled = false
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {

implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}

publishing {
publications {
create<MavenPublication>("release") {
groupId = "com.github.appunite.MockWebServer-Extensions"
artifactId = "mockwebserver-allow-mocking"
version = "0.2.0"

afterEvaluate {
from(components["release"])
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application android:networkSecurityConfig="@xml/network_security_config">
</application>
</manifest>
</manifest>
1 change: 1 addition & 0 deletions mockwebserver-assertions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
30 changes: 30 additions & 0 deletions mockwebserver-assertions/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
plugins {
id("java-library")
id("org.jetbrains.kotlin.jvm")
id("maven-publish")
}

publishing {
publications {
create<MavenPublication>("maven") {
groupId = "com.github.appunite.MockWebServer-Extensions"
artifactId = "mockwebserver-assertions"
version = "0.2.0"

afterEvaluate {
from(components["java"])
}
}
}
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

dependencies {
implementation(project(":mockwebserver-request"))
implementation("com.squareup.okhttp3:okhttp:4.12.0")
implementation("io.strikt:strikt-mockk:0.34.1")
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.appunite.mockwebserverextensions.util
package com.appunite.mockwebserver_assertions

import com.appunite.mockwebserverextensions.Request
import com.appunite.mockwebserver_request.Request
import okhttp3.Headers
import okhttp3.HttpUrl
import okhttp3.Response
Expand Down
5 changes: 3 additions & 2 deletions mockwebserver-extensions/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ publishing {
create<MavenPublication>("maven") {
groupId = "com.github.appunite.MockWebServer-Extensions"
artifactId = "mockwebserver-extensions"
version = "0.1.1"
version = "0.2.0"

afterEvaluate {
from(components["java"])
Expand All @@ -29,10 +29,11 @@ kotlin {

dependencies {
implementation(project(":mockwebserver-interceptor"))
implementation("com.squareup.okhttp3:okhttp:4.12.0")
implementation(project(":mockwebserver-request"))
implementation("com.squareup.okhttp3:mockwebserver:4.12.0")
implementation("io.strikt:strikt-mockk:0.34.1")

testImplementation(project(":mockwebserver-assertions"))
testImplementation("io.mockk:mockk:1.13.5")
testImplementation("junit:junit:4.13.2")
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.appunite.mockwebserverextensions

import com.appunite.mockwebserver_request.Request
import com.appunite.mockwebserverextensions.util.MultipleFailuresError
import com.appunite.mockwebserverextensions.util.ResponseGenerator
import okhttp3.HttpUrl.Companion.toHttpUrl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.appunite.mockwebserverextensions.intercept.UrlOverrideInterceptor
import com.appunite.mockwebserverextensions.util.ResponseGenerator
import okhttp3.Interceptor
import okhttp3.internal.closeQuietly
import okhttp3.mockwebserver.Dispatcher
import okhttp3.mockwebserver.MockWebServer
import org.junit.rules.TestWatcher
import org.junit.runner.Description
Expand All @@ -19,28 +20,27 @@ open class MockWebServerRule(
val LOG: Logger = Logger.getLogger(MockWebServerRule::class.java.name)
}

open var mockDispatcher: MockDispatcher = MockDispatcher()
open var mockWebServer: MockWebServer? = null
val mockDispatcher: MockDispatcher = MockDispatcher()
val mockWebServer: MockWebServer = MockWebServer().apply {
dispatcher = wrapDispatcher(mockDispatcher)
}

override fun register(response: ResponseGenerator) = mockDispatcher.register(response)

override fun clear() = mockDispatcher.clear()

override fun starting(description: Description?) {
val server = MockWebServer()
server.dispatcher = mockDispatcher
TestInterceptor.testInterceptor = interceptor ?: UrlOverrideInterceptor(server.url("/"))
TestInterceptor.testInterceptor = interceptor ?: UrlOverrideInterceptor(mockWebServer.url("/"))
LOG.info("TestInterceptor installed")
mockWebServer = server
}

override fun finished(description: Description) {
LOG.info("TestInterceptor uninstalled")
TestInterceptor.testInterceptor = null

mockWebServer?.closeQuietly()
mockWebServer = null

mockWebServer.closeQuietly()
MultipleFailureException.assertEmpty(mockDispatcher.errors)
}

open fun wrapDispatcher(mockDispatcher: Dispatcher): Dispatcher = mockDispatcher
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.appunite.mockwebserverextensions.util

import com.appunite.mockwebserverextensions.Request
import com.appunite.mockwebserver_request.Request
import okhttp3.mockwebserver.MockResponse
import org.intellij.lang.annotations.Language

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.appunite.mockwebserverextensions

import com.appunite.mockwebserver_assertions.body
import com.appunite.mockwebserver_assertions.bodyString
import com.appunite.mockwebserver_assertions.code
import com.appunite.mockwebserver_assertions.header
import com.appunite.mockwebserver_assertions.headers
import com.appunite.mockwebserver_assertions.host
import com.appunite.mockwebserver_assertions.method
import com.appunite.mockwebserver_assertions.path
import com.appunite.mockwebserver_assertions.url
import com.appunite.mockwebserver_assertions.utf8
import com.appunite.mockwebserver_interceptor.TestInterceptor
import com.appunite.mockwebserver_request.Request
import com.appunite.mockwebserverextensions.util.MultipleFailuresError
import com.appunite.mockwebserverextensions.util.ResponseGenerator
import com.appunite.mockwebserverextensions.util.body
import com.appunite.mockwebserverextensions.util.bodyString
import com.appunite.mockwebserverextensions.util.code
import com.appunite.mockwebserverextensions.util.header
import com.appunite.mockwebserverextensions.util.headers
import com.appunite.mockwebserverextensions.util.host
import com.appunite.mockwebserverextensions.util.jsonResponse
import com.appunite.mockwebserverextensions.util.method
import com.appunite.mockwebserverextensions.util.path
import com.appunite.mockwebserverextensions.util.url
import com.appunite.mockwebserverextensions.util.utf8
import io.mockk.CapturingSlot
import io.mockk.every
import io.mockk.mockk
Expand Down
2 changes: 1 addition & 1 deletion mockwebserver-interceptor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ publishing {
create<MavenPublication>("maven") {
groupId = "com.github.appunite.MockWebServer-Extensions"
artifactId = "mockwebserver-interceptor"
version = "0.1.1"
version = "0.2.0"

afterEvaluate {
from(components["java"])
Expand Down
1 change: 1 addition & 0 deletions mockwebserver-request/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
28 changes: 28 additions & 0 deletions mockwebserver-request/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
plugins {
id("java-library")
id("org.jetbrains.kotlin.jvm")
id("maven-publish")
}

publishing {
publications {
create<MavenPublication>("maven") {
groupId = "com.github.appunite.MockWebServer-Extensions"
artifactId = "mockwebserver-request"
version = "0.2.0"

afterEvaluate {
from(components["java"])
}
}
}
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

dependencies {
implementation("com.squareup.okhttp3:okhttp:4.12.0")
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.appunite.mockwebserverextensions
package com.appunite.mockwebserver_request

import okhttp3.Headers
import okhttp3.HttpUrl
Expand Down
3 changes: 3 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ rootProject.name = "MockWebServer Extensions"
include(":app")
include(":mockwebserver-extensions")
include(":mockwebserver-interceptor")
include(":mockwebserver-allow-mocking")
include(":mockwebserver-assertions")
include(":mockwebserver-request")

0 comments on commit 475275f

Please sign in to comment.