Skip to content

Instantly share code, notes, and snippets.

@tyrcho
Created September 24, 2019 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyrcho/23fb831e04e803c093d1a5860f67411f to your computer and use it in GitHub Desktop.
Save tyrcho/23fb831e04e803c093d1a5860f67411f to your computer and use it in GitHub Desktop.
Demo of a mock HTTP server in scala (uses ammonite)
import $ivy.`com.squareup.okhttp:mockwebserver:2.5.0`
import com.squareup.okhttp.mockwebserver._
val ws = new MockWebServer
ws.start()
val url = ws.url("/test")
println(url)
val response = new MockResponse
response.setBody( """ { "key" : "value" } """)
response.setHeader("content-type", "application/json")
ws.enqueue(response)
val resp=requests.post(url.toString,
headers = Map("Content-Type" -> "application/json"),
data = ujson.Obj("email" -> "user@example.com").render())
println(resp.text)
val request = ws.takeRequest()
println(request.getHeaders)
println(request.getPath)
println(request.getMethod)
println(request.getBody.readUtf8)
ws.shutdown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment