|
| 1 | +# MockWebServer Extensions - Development Guidelines |
| 2 | + |
| 3 | +This document provides essential information for developers working on the MockWebServer Extensions project. |
| 4 | + |
| 5 | +## Build/Configuration Instructions |
| 6 | + |
| 7 | +### Prerequisites |
| 8 | +- JDK 11 or higher |
| 9 | +- Android SDK with API level 34 (compileSdk) |
| 10 | +- Gradle 8.x |
| 11 | + |
| 12 | +### Project Setup |
| 13 | +1. Clone the repository |
| 14 | +2. Open the project in Android Studio or IntelliJ IDEA |
| 15 | +3. Sync the project with Gradle files |
| 16 | + |
| 17 | +### Building the Project |
| 18 | +- To build the entire project: `./gradlew build` |
| 19 | +- To build a specific module: `./gradlew :moduleName:build` |
| 20 | + - Example: `./gradlew :mockwebserver-extensions:build` |
| 21 | + |
| 22 | +### Publishing |
| 23 | +The library is configured for Maven publishing. To publish locally: |
| 24 | +``` |
| 25 | +./gradlew publishToMavenLocal |
| 26 | +``` |
| 27 | + |
| 28 | +## Testing Information |
| 29 | + |
| 30 | +### Running Tests |
| 31 | +- Run all tests: `./gradlew test` |
| 32 | +- Run tests for a specific module: `./gradlew :moduleName:test` |
| 33 | + - Example: `./gradlew :mockwebserver-extensions:test` |
| 34 | + |
| 35 | +### Writing Tests |
| 36 | +The project uses JUnit 4 for testing, along with Strikt for assertions and Mockk for mocking. |
| 37 | + |
| 38 | +#### Basic Test Structure |
| 39 | +```kotlin |
| 40 | +class YourTest { |
| 41 | + @get:Rule |
| 42 | + val mockWebServerRule: MockWebServerRule = MockWebServerRule() |
| 43 | + |
| 44 | + @Test |
| 45 | + fun testSomething() { |
| 46 | + // Register a mock response |
| 47 | + mockWebServerRule.register { request -> |
| 48 | + // You can inspect the request here |
| 49 | + MockResponse().setBody("Your response") |
| 50 | + } |
| 51 | + |
| 52 | + // Execute your code that makes HTTP requests |
| 53 | + val result = yourCodeThatMakesHttpRequests() |
| 54 | + |
| 55 | + // Assert the results |
| 56 | + expectThat(result).isEqualTo(expectedValue) |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +#### Key Testing Components |
| 62 | +1. **MockWebServerRule**: JUnit rule that sets up and manages a MockWebServer instance |
| 63 | + - Automatically installs and uninstalls the TestInterceptor |
| 64 | + - Provides methods to register mock responses |
| 65 | + - Reports errors that occurred during tests |
| 66 | + |
| 67 | +2. **TestInterceptor**: Intercepts HTTP requests and redirects them to the MockWebServer |
| 68 | + - Automatically installed by MockWebServerRule |
| 69 | + - Can be customized with your own interceptor |
| 70 | + |
| 71 | +3. **Assertions**: Custom assertions for HTTP responses |
| 72 | + - `code`: Assert response status code |
| 73 | + - `bodyString`: Assert response body as string |
| 74 | + - `headers`: Assert response headers |
| 75 | + - And many more in the mockwebserver-assertions module |
| 76 | + |
| 77 | +### Example Test |
| 78 | +Here's a simple test that demonstrates how to use the MockWebServerRule: |
| 79 | + |
| 80 | +```kotlin |
| 81 | +@Test |
| 82 | +fun testSimpleRequest() { |
| 83 | + // Register a mock response for any request |
| 84 | + mockWebServerRule.register { _ -> |
| 85 | + MockResponse().setBody("Hello, MockWebServer!") |
| 86 | + } |
| 87 | + |
| 88 | + // Execute a request |
| 89 | + val client = OkHttpClient.Builder() |
| 90 | + .addInterceptor(TestInterceptor) |
| 91 | + .build() |
| 92 | + val request = okhttp3.Request.Builder() |
| 93 | + .url("https://example.com/test") |
| 94 | + .build() |
| 95 | + val response = client.newCall(request).execute() |
| 96 | + |
| 97 | + // Verify the response |
| 98 | + expectThat(response).and { |
| 99 | + code.isEqualTo(200) |
| 100 | + bodyString.isEqualTo("Hello, MockWebServer!") |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +## Additional Development Information |
| 106 | + |
| 107 | +### Project Structure |
| 108 | +The project is organized into several modules: |
| 109 | +- **app**: Sample Android application |
| 110 | +- **mockwebserver-extensions**: Core library with MockWebServerRule and related components |
| 111 | +- **mockwebserver-interceptor**: Contains the TestInterceptor for redirecting HTTP requests |
| 112 | +- **mockwebserver-assertions**: Custom assertions for HTTP responses |
| 113 | +- **mockwebserver-allow-mocking**: Android configuration for allowing HTTP traffic to MockWebServer |
| 114 | +- **mockwebserver-request**: Request model and utilities |
| 115 | + |
| 116 | +### Dependency Management |
| 117 | +The project uses Gradle version catalogs (libs.versions.toml) for dependency management. |
| 118 | + |
| 119 | +### Code Style |
| 120 | +- The project follows Kotlin coding conventions |
| 121 | +- Use extension functions for adding functionality to existing classes |
| 122 | +- Write comprehensive tests for all new features |
| 123 | + |
| 124 | +### Common Patterns |
| 125 | +1. **Response Generation**: Use the `ResponseGenerator` functional interface to create mock responses |
| 126 | +2. **Request Assertions**: Use the assertion extensions from mockwebserver-assertions to verify request properties |
| 127 | +3. **Network Simulation**: Use `simulateNetworkDown()` and `simulateNetworkUp()` methods to test network failure scenarios |
| 128 | + |
| 129 | +## For AI: Generating a Pull Request |
| 130 | + |
| 131 | +To generate a pull request description: |
| 132 | + |
| 133 | +1. Read the content of the pull request template file located at `.github/PULL_REQUEST_TEMPLATE.md`. |
| 134 | +2. Get the code changes by running the command `git diff origin/HEAD...HEAD`. |
| 135 | +3. Analyze the changes and fill out the template with a relevant title, description, and checklist selections. |
| 136 | +4. Save the final output to a temporary file named `pr-description.tmp.md`. |
| 137 | +5. Ensure at the first line of `pr-description.tmp.md` there is a title of the pull request. |
0 commit comments