Create a gist now

Instantly share code, notes, and snippets.

mountebank stubs with F#
open FSharp.Data
open System
open System.Net
open Xunit
let verify = Swensen.Unquote.Assertions.test
[<Fact>]
[<UseImposterStub(
"192.168.1.4",
2525,
@"
{
""port"":4545,
""protocol"":""http"",
""stubs"":[
{
""responses"":[
{
""is"":{
""statusCode"":201,
""headers"":{
""Location"":""http://localhost:4545/customers/123"",
""Content-Type"":""application/xml""
},
""body"":""<customer><email>customer@test.com</email></customer>""
}
},
{
""is"":{
""statusCode"":400,
""headers"":{
""Content-Type"":""application/xml""
},
""body"":""<error>email already exists</error>""
}
}
]
}
]
}"
)>]
let CreateDuplicateCustomerThrows () =
let post url body =
Http.Request(
url,
headers = [
"Content-Type", HttpContentTypes.Xml;
"Accept" , HttpContentTypes.Xml ],
httpMethod = "POST",
body = TextRequest body)
let Create resource = post "http://192.168.1.4:4545/imposters/" resource
let mutable secondRequestHasFailed = false
let actual =
Create "<customer><email>customer@test.com</email></customer>"
try
Create "<customer><email>customer@test.com</email></customer>"
|> ignore
with
| e -> if e.Message.Contains("email already exists")
then secondRequestHasFailed <- true
verify <@ actual.StatusCode = 201 && secondRequestHasFailed @>
open System
open System.Reflection
open FSharp.Data
open Xunit
type UseImposterStubAttribute (mountebankHost, mountebankPort, imposterJson) =
inherit BeforeAfterTestAttribute()
override this.Before (methodUnderTest : MethodInfo) =
Http.Request(
UriBuilder(
"http",
mountebankHost,
mountebankPort,
"imposters/").ToString(),
headers = [
"Content-Type", HttpContentTypes.Json;
"Accept" , HttpContentTypes.Json ],
httpMethod = "POST",
body = TextRequest imposterJson)
|> ignore
override this.After (methodUnderTest : MethodInfo) =
Http.Request(
UriBuilder(
"http",
mountebankHost,
mountebankPort,
"imposters/" + ParsePortFrom(imposterJson)).ToString(),
headers = [
"Content-Type", HttpContentTypes.Json;
"Accept" , HttpContentTypes.Json ],
httpMethod = "DELETE")
|> ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment