httpmock

Real http server for stubbing and expectations in Scala

License

License

GroupId

GroupId

com.github.xuwei-k
ArtifactId

ArtifactId

httpmock_2.11
Last Version

Last Version

0.4.0
Release Date

Release Date

Type

Type

jar
Description

Description

httpmock
Real http server for stubbing and expectations in Scala
Project URL

Project URL

https://github.com/xuwei-k/httpmock
Project Organization

Project Organization

com.github.xuwei-k
Source Code Management

Source Code Management

https://github.com/xuwei-k/httpmock

Download httpmock_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.xuwei-k/httpmock_2.11/ -->
<dependency>
    <groupId>com.github.xuwei-k</groupId>
    <artifactId>httpmock_2.11</artifactId>
    <version>0.4.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.xuwei-k/httpmock_2.11/
implementation 'com.github.xuwei-k:httpmock_2.11:0.4.0'
// https://jarcasting.com/artifacts/com.github.xuwei-k/httpmock_2.11/
implementation ("com.github.xuwei-k:httpmock_2.11:0.4.0")
'com.github.xuwei-k:httpmock_2.11:jar:0.4.0'
<dependency org="com.github.xuwei-k" name="httpmock_2.11" rev="0.4.0">
  <artifact name="httpmock_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.xuwei-k', module='httpmock_2.11', version='0.4.0')
)
libraryDependencies += "com.github.xuwei-k" % "httpmock_2.11" % "0.4.0"
[com.github.xuwei-k/httpmock_2.11 "0.4.0"]

Dependencies

compile (2)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.11
com.typesafe.play : play-netty-server_2.11 jar 2.6.2

test (2)

Group / Artifact Type Version
org.scalatest : scalatest_2.11 jar 3.0.3
com.ning : async-http-client jar 1.9.40

Project Modules

There are no modules declared in this project.

Maven Central scaladoc

HttpMock

Real http server for stubbing and expectations in Scala

Features

  • Stubbing HTTP requests at real Http Server
    • responds always OK for any methods and paths
    • supported methods: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS
  • Expecting HTTP requests as AccessLog
    • asserts AccessLog about methods and counts

Stubbing

start (random port)

import httpmock._
val server = HttpMock.start()
server.port  // => 37781 (automatically set by default)
// send requests to "http://127.0.0.1:37781" (or server.url)
server.stop

start with port

val server = HttpMock.start(9000)
server.port  // => 9000

restartable

  • HttpMock is immutable
val server    = HttpMock.start(9000)  // GET: ok
val stopped   = server.stop()         // GET: error
val restarted = stopped.start()       // GET: ok

loan pattern

  • run() ensures server.stop() after action
HttpMock.run()      { server => ... }
HttpMock.run(port)  { server => ... }
Setting(port).run() { server => ... }

restrict methods

  • default: accept all methods
val server = Setting(methods = Set(GET, POST)).start()
/*
  GET  => 200
  POST => 200
  PUT  => 404
*/

customize request handler

val server = Setting(handler = {
    case h: RequestHeader if h.version == "HTTP/1.0" => Results.HttpVersionNotSupported
})
/*
  GET  => 200
  GET with HTTP/1.0 => 505
*/

Expectations

  • implemented expectations:
    • method, body, header, count
import httpmock._
import scala.concurrent.duration._

val server = HttpMock.start(9000)

assert methods and counts

curl http://127.0.0.1:9000/
server.logs.expect(GET , count = 1)(1.second)  // (PASS)
server.logs.expect(GET , count = 2)(1.second)  // java.lang.AssertionError
server.logs.expect(POST, count = 1)(1.second)  // java.lang.AssertionError

server.stop()

assert methods and body and headers

curl -X POST -H "Content-type: application/octet-stream" http://127.0.0.1:9000/ -d foo
curl -X POST -H "Content-type: application/octet-stream" http://127.0.0.1:9000/ -d bar
curl -X POST -H "Content-type: application/octet-stream" -H "X-ID: 1" http://127.0.0.1:9000/ -d bar
server.logs.expect(POST).body("foo")(1.second)           // (PASS)
server.logs.expect(POST).body("bar")(1.second)           // java.lang.AssertionError
server.logs.expect(POST).body("bar").count(2)(1.second)  // (PASS)
server.logs.expect(POST).body("baz")(1.second)           // java.lang.AssertionError
server.logs.expect(POST).header("X-ID", "1")(1.second)   // (PASS)

server.stop()

assert bodies as Set

curl -X POST -H "Content-type: application/octet-stream" http://127.0.0.1:9000/ -d foo
curl -X POST -H "Content-type: application/octet-stream" http://127.0.0.1:9000/ -d bar
curl -X POST -H "Content-type: application/octet-stream" -H "X-ID: 1" http://127.0.0.1:9000/ -d bar
server.logs.expect(POST).bodies(Set("foo", "bar"))(1.second)  // (PASS)
server.logs.expect(POST).bodies(Set("bar", "foo"))(1.second)  // (PASS)
server.logs.expect(POST).bodies(Set("foo", "XXX"))(1.second)  // java.lang.AssertionError

server.stop()

using in Spec

import httpmock._
import scala.concurrent.duration._
import org.scalatest.FunSpec

class FooSpec extends FunSpec {
  describe("foo") {
    it("test with real httpd") {
      HttpMock.run { server =>
        // your application logic to `server.url`
        ...

        // assert your requests like this
        server.logs.expect(POST, count = 2)(3.seconds)
      }
    }
  }
}

Versions

Version
0.4.0