Last active
August 29, 2015 14:00
-
-
Save moodmosaic/1fbad33d03b11b188edc to your computer and use it in GitHub Desktop.
mountebank stubs with F#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 @> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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