Giter VIP home page Giter VIP logo

livestub's Introduction

livestub

Build Status Docker Pulls Docker Image Size (latest by date) Scala Steward badge Maven Central

With livestub you can easly setup http server that behaves exactly as you would like it to. That makes livestub a perfect solution for faking arbitrary services which might be very useful in development/testing.

Usage

launch

  • coursier

    coursier launch com.softwaremill.sttp.livestub:livestub-app_2.13:0.2.1 -- -p 7070

  • docker

    docker run -p 7070:7070 softwaremill/sttp.livestub

  • code

import sttp.livestub.app.LiveStubServer
import sttp.livestub.app.LiveStubServer.Config
LiveStubServer.resource(Config(port = 7070))

stub

curl -X POST 'localhost:7070/__set' \
-d '{"when":{"method":"GET", "url":"animals/1/status"}, "then": {"statusCode":200, "body":{"status": "happy"} }}'

invoke

curl 'localhost:7070/animals/1/status'
{"status":"happy"}

swagger

Swagger is available under /__admin/docs

advanced stubbing

Livestub supports wildcard http methods as well as wildcard path parameters.

wildcard method:

curl -X POST 'localhost:7070/__set' \
-d '{"when":{"method":"*", "url":"dogs"}, "then": {"statusCode":200, "body":{"status": "OK"} }}'

wildcard path param:

curl -X POST 'localhost:7070/__set' \
-d '{"when":{"method":"GET", "url":"dogs/*/status"}, "then": {"statusCode":200, "body":{"status": "happy"} }}'

multiwildcard path param: (this one catches all routes which originate from /dogs)

curl -X POST 'localhost:7070/__set' \
-d '{"when":{"method":"GET", "url":"dogs/**"}, "then": {"statusCode":200, "body":{"status": "cheerful"} }}'

wildcard query param: (this one catches all the query parameters for /dogs, example: /dogs?id=1&breed=bulldog)

curl -X POST 'localhost:7070/__set' \
-d '{"when":{"method":"GET", "url":"dogs?*"}, "then": {"statusCode":200, "body":{"status": "cheerful"} }}'

additional methods

clear stubbed routes

curl -X POST 'localhost:7070/__clear'

show stubbed routes

curl 'localhost:7070/__routes'

set many responses which will be cycled through

curl -X POST 'localhost:7070/__set_many' \
-d '{"when":{"method":"GET", "url":"animals/1/status"}, "then": [
    {"statusCode":200, "body":{"status": "happy"} },
    {"statusCode":200, "body":{"status": "unhappy"} }
  ]
}'

stubbing from code - sdk

libraryDependencies += "com.softwaremill.sttp.livestub" % "livestub-sdk" % "0.2.1"

Given a bunch of imports

import sttp.client3.asynchttpclient.cats.AsyncHttpClientCatsBackend
import sttp.client3.SttpBackend
import sttp.livestub.sdk._
import sttp.livestub.api._
import sttp.client3.{Response => _, _}
import sttp.model._
import io.circe.Json

you can stub an arbitrary request:

AsyncHttpClientCatsBackend[IO]().flatMap { backend: SttpBackend[IO, Any] =>
  val livestub = new LiveStubSdk[IO](uri"http://mock:7070", backend)
  livestub
    .when(RequestStubIn(MethodStub.Wildcard, "/user/*/status"))
    .thenRespond(Response.emptyBody(StatusCode.Ok, List(Header("X-App", "123"))))
}

stub given sttp request:

AsyncHttpClientCatsBackend[IO]().flatMap { backend: SttpBackend[IO, Any] =>
  val request = basicRequest
    .body(Map("name" -> "John", "surname" -> "doe"))
    .post(uri"https://httpbin.org/post?signup=123")

  val livestub = new LiveStubSdk[IO](uri"http://mock:7070", backend)
  livestub.when(request).thenRespond(Response(Some(Json.fromString("OK")), StatusCode.Ok))
}

or stub given tapir endpoint:

import sttp.tapir._

AsyncHttpClientCatsBackend[IO]().flatMap { backend: SttpBackend[IO, Any] =>
  val myEndpoint = endpoint.get.in("/status").out(stringBody)

  val livestub = new LiveStubSdk[IO](uri"http://mock:7070", backend)
  livestub.when(myEndpoint)
          .thenRespond(Response.emptyBody(StatusCode.Ok, List(Header("X-App", "123"))))
}

OpenApi integration

OpenApi specification can be provided to bootstrap livestub server with auto generated endpoints. For each given path a request-response stub will be generated matching particular url. Both path parameters and required query parameters will be replaced by wildcard matches. That means that neither optional query parameters, request body nor headers are going to be checked when matching stubbed endpoint.

Response structure will follow openapi schema. Response data will be created based on openapi respective example parameters if provided, otherwise random data will be used(whenever it is possible).

For endpoints, which are defined in openapi specification but for which responses couldn't be generated empty response body will be returned.

Usage: --openapi-spec <path_to_file>

In addition, seed value for random data generator can be passed via: --seed 1234

Development

Documentation is generated during every release from docs-sources directory. It can be manually regenerated when needed by calling sbt docs/mdoc.

To release new version use sbt release.

livestub's People

Contributors

dependabot[bot] avatar ghostbuster91 avatar matzz avatar mergify[bot] avatar mkrzemien avatar paulpdaniels avatar prabhugopal avatar scala-steward avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

livestub's Issues

SBT libraryDependencies example is broken

This is a trivial issue, but the "stubbing from code" section of the readme contains the following example for adding livestub as a dependency:
libraryDependencies += "com.softwaremill.sttp.livestub" % "livestub-sdk" % "0.1.18"

This does not work as-written, due to not specifying the Scala version. It should be:
libraryDependencies += "com.softwaremill.sttp.livestub" %% "livestub-sdk" % "0.1.18"

Run livestub server from openapi spec json file

I'm trying to run livestub server from openapi file by using command:
coursier launch com.softwaremill.sttp.livestub:livestub-app_2.13:0.2.1 -- -p 7070 --openapi-spec openApi.json

I'm receiving error:
java.lang.RuntimeException: DecodingFailure(Missing required field, List(DownField(version), DownField(info))) at sttp.livestub.app.LiveStubLauncher$.$anonfun$main$1(LiveStubLauncher.scala:33) at sttp.livestub.app.LiveStubLauncher$.$anonfun$main$1$adapted(LiveStubLauncher.scala:29) at cats.SemigroupalArityFunctions.$anonfun$map4$1(SemigroupalArityFunctions.scala:60) at cats.data.Validated.ap(Validated.scala:522) at cats.data.ValidatedApplicative.ap(Validated.scala:1082) at cats.data.ValidatedApplicative.ap(Validated.scala:1075) at cats.ComposedApply.$anonfun$ap$2(Composed.scala:54) at cats.instances.Function0Instances$$anon$4.$anonfun$ap$1(function.scala:105) at com.monovore.decline.Parser.evalResult(Parser.scala:30) at com.monovore.decline.Parser.consumeAll(Parser.scala:116) at com.monovore.decline.Parser.apply(Parser.scala:19) at com.monovore.decline.Command.parse(opts.scala:20) at com.monovore.decline.effect.CommandIOApp$.$anonfun$run$4(CommandIOApp.scala:45) at use @ sttp.livestub.app.LiveStubLauncher$.$anonfun$main$1(LiveStubLauncher.scala:39) at map @ com.monovore.decline.effect.CommandIOApp$.$anonfun$run$7(CommandIOApp.scala:48) at delay @ com.monovore.decline.effect.CommandIOApp$.run(CommandIOApp.scala:43) at flatMap @ com.monovore.decline.effect.CommandIOApp$.run(CommandIOApp.scala:42) Caused by: DecodingFailure(Missing required field, List(DownField(version), DownField(info)))

What i'm doing wrong?

docker - invalid reference format

when trying to publish docker image from a snapshot version following error occurs:

[error] invalid argument "softwaremill/sttp.livestub:0.1.11+242-3d4a132f-SNAPSHOT" for "-t, --tag" flag: invalid reference format

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.