Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
adarmus opened this issue Nov 13, 2018 · 7 comments
Closed

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

adarmus opened this issue Nov 13, 2018 · 7 comments
Assignees

Comments

@adarmus
Copy link
Contributor

adarmus commented Nov 13, 2018

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
Copy link

marktilleytlc commented Nov 13, 2018

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
Copy link
Contributor Author

adarmus commented Nov 13, 2018

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

Adam

@djmnz
Copy link
Owner

djmnz commented Nov 16, 2018

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
Copy link
Contributor Author

adarmus commented Nov 16, 2018

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

Thanks, Adam

@djmnz
Copy link
Owner

djmnz commented Nov 18, 2018

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();

@djmnz djmnz closed this as completed Nov 18, 2018
@djmnz djmnz self-assigned this Nov 18, 2018
@marktilleytlc
Copy link

marktilleytlc commented Nov 20, 2018 via email

@adarmus
Copy link
Contributor Author

adarmus commented Nov 20, 2018

Great - thanks.

Adam

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants