Practice ยท 2 of 3
Service Over a Fake Transport
Model in Solution:
- record HttpReply(int status, String body)
- interface HttpTransport { HttpReply send(String url); }
- static class ApiClient with constructor (HttpTransport t) and
String fetchUser(String id):
- sends to "https://api.test/users/" + id
- 200 โ returns body
- 404 โ throws NoSuchElementException("user " + id)
- 5xx โ throws IllegalStateException("server error " + status)
- static HttpTransport fakeTransport(Map<String, HttpReply> replies) โ
a transport returning the canned reply for the matching URL.
The tests never open a socket; the fake IS the point.
Difficulty: intermediate
Press Submit to check your solution.
Back to lesson: Practice: Async & HTTP Lab