djmnz / RestFluencing

MIT License
11 stars 4 forks source link

How to get the API response to use in subsequent tests? #2

Closed adarmus closed 5 years ago

adarmus commented 5 years ago

Hi - love the library.

We want to use the ID from one request to construct a second request. How can we access the response model from the first outside of an "assert" expression?

Thanks, Adam

marktilleytlc commented 5 years ago

Hi, I work with Adam. If we just add a property to RestResponse:

public IApiClientResponse ApiResponse => Context.Response;

Then we can: var myResponse = JsonConvert.DeserializeObject< MyResponseModel>(response.ApiResponse.Content); and access anything we need.

Thanks, Mark

adarmus commented 5 years ago

I have added a couple of PRs that allow this to be done (in different ways).

Adam

djmnz commented 5 years ago

hey guys @adarmus @marktilleytlc Thanks for the feedback and contribution!

As you figured it out, there is not built in way to do what you need. And thanks for creating the Action Rule.

However I feel that "Action" breaks a little bit the "Fluent" syntax and the Assertion intention of the framework.

I understand the benefit of an action before and after a request, but also an easy way for people to add custom validations.

So do you reckon that the example below would be helpful for you all?


string headerValue = "";
string login = "";
Rest.GetFromUrl("https://api.github.com/users/defunkt")
    .WithHeader("User-Agent", "RestFluencing Sample")
    .BeforeRequestDo(context => {
        // you can intercept the request for whatever reason
    })
    .AfterRequestDo(context => {
        // you can intercept the response for whatever reason
    })
    .Response()
    .Returns<GitHubUserModel>(model =>
    {
        // Here you can do action but also do inline specific validations
        // but also access the deserialized response
        login = model.login;
        return true;
    }, "Your reason why it failed - in your case you leave it blank since would never fail.")
    .ReturnsStatus(HttpStatusCode.OK)
    .Assert();

I will see if I can crack some of this code this weekend :)

adarmus commented 5 years ago

Hi - I understand your concerns, and your suggestion looks good.

Thanks, Adam

djmnz commented 5 years ago

I have uploaded the new package that includes the before and after request events and also a ReturnsModel that allows you to write inline validation.

You should still be able to use your custom rules if you added them as part of your project.

    // Arrange
    bool callFromRequest = false;
    var config = RestConfigurationHelper.Default();
    var request = config.Get("/null")
        .AfterRequest(context => { callFromRequest = true; });
    // Act
    request.Response().Assert();
    // Assert
    Assert.IsTrue(callFromRequest);

And

    Rest.Get("/product/apple", _configuration)
        .Response()
        .ReturnsModel<Product>(product => {
            return
                product.Name == "Apple";
        }, "My custom error message")
        .Execute()
        .ShouldPass();
marktilleytlc commented 5 years ago

Thank you Douglas, I have updated our code to use AfterRequest and it is working well.

Regards

Mark

From: Douglas Jimenez notifications@github.com Sent: 18 November 2018 03:20 To: djmnz/RestFluencing RestFluencing@noreply.github.com Cc: Mark Tilley Mark.Tilley@systemc.com; Mention mention@noreply.github.com Subject: Re: [djmnz/RestFluencing] How to get the API response to use in subsequent tests? (#2)

I have uploaded the new package that includes the before and after request events and also a ReturnsModel that allows you to write inline validation.

You should still be able to use your custom rules if you added them as part of your project.

    // Arrange

    bool callFromRequest = false;

    var config = RestConfigurationHelper.Default();

    var request = config.Get("/null")

           .AfterRequest(context => { callFromRequest = true; });

    // Act

    request.Response().Assert();

    // Assert

    Assert.IsTrue(callFromRequest);

And

    Rest.Get("/product/apple", _configuration)

           .Response()

           .ReturnsModel<Product>(product => {

                   return

                           product.Name == "Apple";

           }, "My custom error message")

           .Execute()

           .ShouldPass();

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/djmnz/RestFluencing/issues/2#issuecomment-439664594, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ANhW6ExRoqXlCwnkmxovAUFpCSLp0qcRks5uwNHYgaJpZM4Ybj-Y.

This email, and any attachments transmitted with it, is confidential and protected by copyright. If you are not the intended recipient you must not use, disseminate, distribute or copy this e-mail or any attachment. If you have received this email in error please notify the sender and delete this email, and any attachments, completely from your system. Any views or opinions expressed within this email are those of the author, and do not necessarily represent those of System C. System C has scanned this email for viruses but does not accept any responsibility once this email has been transmitted. You should scan attachments (if any) for viruses. Registered Address: System C Healthcare Ltd, The Maidstone Studios, Vinters Business Park, New Cut Road, Maidstone, Kent ME14 5NZ. Registered in England: 1754990

adarmus commented 5 years ago

Great - thanks.

Adam