|
| 1 | +--- |
| 2 | +id: espresso-mockwebserver-localhost |
| 3 | +title: Testing with MockWebServer & Localhost |
| 4 | +sidebar_label: MockWebServer & Localhost |
| 5 | +description: Test applications using MockWebServer or localhost-based mock servers with Espresso on TestMu AI real devices. |
| 6 | +keywords: |
| 7 | + - espresso |
| 8 | + - mockwebserver |
| 9 | + - localhost testing |
| 10 | + - port forwarding |
| 11 | + - mock server |
| 12 | + - real devices |
| 13 | +url: https://www.testmuai.com/support/docs/espresso-mockwebserver-localhost/ |
| 14 | +site_name: TestMu AI |
| 15 | +slug: espresso-mockwebserver-localhost/ |
| 16 | +canonical: https://www.testmuai.com/support/docs/espresso-mockwebserver-localhost/ |
| 17 | +--- |
| 18 | + |
| 19 | +import CodeBlock from '@theme/CodeBlock'; |
| 20 | +import {YOUR_LAMBDATEST_USERNAME, YOUR_LAMBDATEST_ACCESS_KEY} from "@site/src/component/keys"; |
| 21 | +import BrandName, { BRAND_URL } from '@site/src/component/BrandName'; |
| 22 | + |
| 23 | +<script type="application/ld+json" |
| 24 | + dangerouslySetInnerHTML={{ __html: JSON.stringify({ |
| 25 | + "@context": "https://schema.org", |
| 26 | + "@type": "BreadcrumbList", |
| 27 | + "itemListElement": [{ |
| 28 | + "@type": "ListItem", |
| 29 | + "position": 1, |
| 30 | + "name": "Home", |
| 31 | + "item": BRAND_URL |
| 32 | + },{ |
| 33 | + "@type": "ListItem", |
| 34 | + "position": 2, |
| 35 | + "name": "Support", |
| 36 | + "item": `${BRAND_URL}/support/docs/` |
| 37 | + },{ |
| 38 | + "@type": "ListItem", |
| 39 | + "position": 3, |
| 40 | + "name": "MockWebServer & Localhost", |
| 41 | + "item": `${BRAND_URL}/support/docs/espresso-mockwebserver-localhost/` |
| 42 | + }] |
| 43 | + }) |
| 44 | + }} |
| 45 | +></script> |
| 46 | + |
| 47 | +<BrandName /> supports testing apps that use MockWebServer or similar localhost-based mock servers in Android Espresso tests. |
| 48 | + |
| 49 | +## Why Special Configuration is Needed |
| 50 | + |
| 51 | +When network capture (`network: true`) is enabled, HTTP requests from the device route through proxy. Localhost requests fail because the proxy runs on the host machine, not the device — so `http://localhost:{port}` resolves to the host's localhost instead of the device's. |
| 52 | + |
| 53 | +Platform provides two solutions: **Localhost Bypass** and **Port Forwarding**. |
| 54 | + |
| 55 | +## Option 1: Localhost Bypass |
| 56 | + |
| 57 | +Works when network is set to `true` and application uses standard HTTP libraries.Localhost network calls will not be captured. |
| 58 | + |
| 59 | +```json |
| 60 | +{ |
| 61 | + "app": "lt://APP_ID", |
| 62 | + "testSuite": "lt://TESTSUITE_ID", |
| 63 | + "device": ["Galaxy S21-12", "Pixel 6-13"], |
| 64 | + "build": "MockWebServer Test", |
| 65 | + "network": true, |
| 66 | + "localhost": true |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +| Capability | Data Type | Description | |
| 71 | +|------------|-----------|-------------| |
| 72 | +| `localhost` | Boolean | Bypass proxy for `localhost`/`127.0.0.1` requests. Default: `false` | |
| 73 | + |
| 74 | +### Supported HTTP Libraries |
| 75 | + |
| 76 | +| Library | Notes | |
| 77 | +|---------|-------| |
| 78 | +| **Java HttpURLConnection** | `URL.openConnection()` is intercepted | |
| 79 | +| **OkHttp** | `proxy()` and `proxySelector()` are intercepted | |
| 80 | +| **Retrofit** | Uses OkHttp internally | |
| 81 | +| **Volley** | Uses HttpURLConnection internally | |
| 82 | + |
| 83 | +:::note Requirements |
| 84 | +- `network: true` is required for `localhost: true` to work. |
| 85 | +- `localhost` capability cannot be used together with `portForwarding`. |
| 86 | +- Supported only on real android devices right now. |
| 87 | + |
| 88 | +## Option 2: Port Forwarding |
| 89 | + |
| 90 | +Best when localhost network logs need to be captured and there are no port conflicts. |
| 91 | + |
| 92 | +```json |
| 93 | +{ |
| 94 | + "app": "lt://APP_ID", |
| 95 | + "testSuite": "lt://TESTSUITE_ID", |
| 96 | + "device": ["Galaxy S21-12", "Pixel 6-13"], |
| 97 | + "build": "MockWebServer Test", |
| 98 | + "network": true, |
| 99 | + "portForwarding": { |
| 100 | + "ports": [9091, 9092] |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +| Capability | Data Type | Description | |
| 106 | +|------------|-----------|-------------| |
| 107 | +| `portForwarding` | Object | Configure port forwarding for localhost services on the device | |
| 108 | +| `portForwarding.ports` | Array | Ports to forward (max 5 unique ports, must be 1024–65535) | |
| 109 | + |
| 110 | +Port forwarding works at the network level, so **all HTTP libraries are supported**. |
| 111 | + |
| 112 | +:::note |
| 113 | +- Ports must be in the range 1024–65535. Privileged ports (1–1023) are blocked. |
| 114 | +- Maximum 5 unique ports. No duplicate ports allowed. |
| 115 | +- Invalid port formats (e.g., strings like `"abc"`) are rejected. |
| 116 | +- Cannot be used together with `localhost`. |
| 117 | +- `network: true` is **not** required for port forwarding — it works independently. |
| 118 | +::: |
| 119 | + |
| 120 | + |
| 121 | +## Troubleshooting |
| 122 | + |
| 123 | +- **Connection errors** — Ensure `network: true` is set when using `localhost: true`. For `portForwarding`, `network: true` is optional. |
| 124 | +- **Cannot use localhost and portForwarding together** — These are mutually exclusive. Pick one. |
| 125 | +- **Port validation errors** — Ports must be 1024–65535, max 5 unique ports, no duplicates allowed. |
| 126 | + |
| 127 | +<nav aria-label="breadcrumbs"> |
| 128 | + <ul className="breadcrumbs"> |
| 129 | + <li className="breadcrumbs__item"> |
| 130 | + <a className="breadcrumbs__link" target="_self" href={BRAND_URL}> |
| 131 | + Home |
| 132 | + </a> |
| 133 | + </li> |
| 134 | + <li className="breadcrumbs__item"> |
| 135 | + <a className="breadcrumbs__link" target="_self" href={`${BRAND_URL}/support/docs/`}> |
| 136 | + Support |
| 137 | + </a> |
| 138 | + </li> |
| 139 | + <li className="breadcrumbs__item breadcrumbs__item--active"> |
| 140 | + <span className="breadcrumbs__link"> |
| 141 | + MockWebServer & Localhost |
| 142 | + </span> |
| 143 | + </li> |
| 144 | + </ul> |
| 145 | +</nav> |
0 commit comments