Create a gist now

Instantly share code, notes, and snippets.

mountebank mocks with F#
namespace Mountebank
// PM> Install-Package FSharp.Data
open FSharp.Data
open System
module Imposter =
let Create protocol host port =
Http.Request(
"http://" + host + ":2525/imposters/",
headers = [
"Content-Type", HttpContentTypes.Json;
"Accept", HttpContentTypes.Json ],
httpMethod = "POST",
body =
TextRequest
(sprintf @"{ ""port"": %i, ""protocol"": ""%s"" }"
port protocol))
let GetCapturedRequests (spy : HttpResponse) =
let getRequests jsonValue =
match jsonValue with
| FSharp.Data.Record properties ->
match properties with
| [| protocol; port; requests; stubs; _links; |] ->
match snd requests with
| FSharp.Data.Array elements ->
match elements |> Seq.toList with
| head :: tail -> Some(elements)
| [] -> None
| _ -> None
| _ -> None
| _ -> None
let response =
Http.Request(
spy.Headers.Item "Location",
headers = [
"Content-Type", HttpContentTypes.Json;
"Accept", HttpContentTypes.Json ],
httpMethod = "DELETE")
match response.Body with
| Text json ->
JsonValue.Parse json
|> getRequests
| _ -> None
module SmtpSpy =
let GetCapturedRequest imposter propertyName =
let filter properties propertyName =
match properties with
| [| _; _; _; _; _; _; _; _; _; _; _; _; _; _ |] ->
(properties
|> Array.filter (fun (name, _) -> name = propertyName)
|> Seq.head
|> snd).ToString().TrimStart('"').TrimEnd('"')
|> Some
| _ -> None
match Imposter.GetCapturedRequests imposter with
| Some actual ->
match actual |> Seq.head with
| FSharp.Data.Record properties -> filter properties propertyName
| _ -> None
| _ -> None
module Mountebank.Scenarios
// PM> Install-Package xunit
// PM> Install-Package Unquote
open System
open System.Net.Mail
open Xunit
let verify = Swensen.Unquote.Assertions.test
[<Fact>]
let SendMailTransmitsCorrectSubject () =
let expectedSubject = "Thank you for your order!"
let mountebankHost = "192.168.1.4"
let imposterPort = 4547
let spy = Imposter.Create "smtp" mountebankHost imposterPort
(new SmtpClient(mountebankHost, imposterPort)).Send(
new MailMessage(
"customer-service@company.com",
"customer@domain.com",
expectedSubject,
"Hello Customer, Thank you for your order from company.com."))
verify <@
match SmtpSpy.GetCapturedRequest spy "subject" with
| Some actual -> expectedSubject = actual
| None ->
raise <| InvalidOperationException(
"No property named 'subject' found in captured requests.") @>
[<Fact>]
let SendMailTransmitsCorrectNumberOfSmtpRequests () =
let expectedNumberOfRequests = 1
let mountebankHost = "192.168.1.4"
let imposterPort = 4546
let spy = Imposter.Create "smtp" mountebankHost imposterPort
(new SmtpClient(mountebankHost, imposterPort)).Send(
new MailMessage(
"customer-service@company.com",
"customer@domain.com",
"Thank you for your order!",
"Hello Customer, Thank you for your order from company.com."))
verify <@
match Imposter.GetCapturedRequests spy with
| Some actual -> expectedNumberOfRequests = (actual |> Seq.length)
| None ->
raise <| InvalidOperationException(
sprintf "Expected %i calls but received none."
expectedNumberOfRequests) @>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment