Skip to content

Instantly share code, notes, and snippets.

@moodmosaic
Last active August 29, 2015 14:00
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 moodmosaic/1fbad33d03b11b188edc to your computer and use it in GitHub Desktop.
Save moodmosaic/1fbad33d03b11b188edc to your computer and use it in GitHub Desktop.
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