Code Monkey home page Code Monkey logo

httpclient.helpers's Introduction

httpclient.helpers's People

Contributors

abatishchev avatar hypertensy avatar purekrome avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

httpclient.helpers's Issues

FakeHttpMessageHandler ctor - Wiki update or do I miss something?

I'm using the last recent version 5.0 and tried to follow the Wiki, but it seems the FakeHttpMessageHandler ctor has changed and the wiki is not up to date.

From the wiki I should do something like this:

        var messageHandler = new FakeHttpMessageHandler(requestUrl, messageResponse);

But there is only one parameter:

image

I did this in my code and it seems to work:

        var messageResponse = FakeHttpMessageHandler.GetStringHttpResponseMessage(responseData, HttpStatusCode.BadRequest);

        var fakeMessageHandler = new FakeHttpMessageHandler(new HttpMessageOptions() { HttpResponseMessage = messageResponse });

Is this the right approach?

Need a way to verify operation was called

If we are doing something like a HTTP DELETE where there is no returned body/headers (usually just a status code), then we need a way in our tests to verify the operation was called (since the calling method will return void).

Suggestion: add WasCalled property (only get property) to HttpMessageOptions, which can then be checked in the test.

e.g

// Arrange.
// ... snip ...
var options = new HttpMessageOptions
{
    HttpMethod = HttpMethod.Delete,
    RequestUri = "http://api.com/user",
    HttpResponseMessage = new HttpResponseMessage(HttpStatusCode.NoContent)
};
var fakeMessageHandler = new FakeHttpMessageHandler(options);

// Act.
var client = new TestClientClient(baseUrl, fakeMessageHandler);

// Act.
await client .DeleteUserAsync(someoperation);

// Assert.
options.WasCalled.ShouldBeTrue();

Dnx support?

Hi I was wondering if this would work in a dnx project? ASP.NET 5 vnext?

Possibility to assert invoked Uris?

Hi,

I'm using your library to author some tests and I got myself in the situation where my API client is forming a URL by adding many components both in the path and the querystring.

My test is targeting only a subsection of the requested URI and I would like to "test" it. Ideally on the used option.

To make a concrete example

var option = new HttpMessageOptions
{
    HttpMethod = HttpMethod.Get,
    HttpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
    {
        Content = new StringContent(JsonConvert.SerializeObject(contact))
    },
    RequestUri = null 
};

var sut = CreateSystemUnderTest(option);

var response = await sut.GetByIdAsync(contactId);

Assert.That(option.InvokedUri, Contains.SubString($"/contacts/v1/contact/vid/{contactId}"));

Right now I am forced to do:

var options = new HttpMessageOptions
{
    HttpMethod = HttpMethod.Get,
    HttpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
    {
        Content = new StringContent(JsonConvert.SerializeObject(contact))
    },
    RequestUri = new Uri($"https://api.hubapi.com/contacts/v1/contact/vid/{contactId}/profile?formSubmissionMode=all&propertyMode=value_and_history&showListMemberships=true")
};

which is really outside of the scope of my unit test.

HttpClientFactory, xUnit2 and parallelism - weird success/failed tests

Hi, I'm using your latest and greatest library, but after I updated to xUnit2 I'm seeing some weird behaviour. Tests that are touching the HttpClientFactory are failing or succeeding randomly, but I'm not sure if it is a "problem" in your helper library or a test design problem.

For example (without any code chances)

image

image

image

Even the console test runner shows mixed results. After some headaches I found the reason: With xUnit2 tests can now run in parallel:
http://xunit.github.io/docs/running-tests-in-parallel.html

Now pure guessing from me; Because HttpClientFactory is a static thing and it is used in parallel from different tests the tests behave very differently.

Is there a way to inject the HttpClientFactory inside the test class, so each test have their own factory?

My code can be found here:
https://github.com/Code-Inside/Sloader

HttpContent not properly checked when selecting expected options

Hi,

I think I found a bug in the FakeMessageHandler when it compares the incoming HttpContent and the expected ones received from the HttpMessageOptions.

The problem is caused by the fact that your equality check is a reference check.

private HttpMessageOptions GetExpectedOption(HttpMessageOptions option)
{
    if (option == null)
    {
        throw new ArgumentNullException(nameof(option));
    }

    IEnumerable<HttpMessageOptions> options = _lotsOfOptions.Values;

    options = options.Where(x => x.RequestUri == option.RequestUri || x.RequestUri == HttpMessageOptions.NoValue);

    options = options.Where(x => (x.HttpMethod == option.HttpMethod || x.HttpMethod == null));

    options = options.Where(x => (x.HttpContent == option.HttpContent || x.HttpContent == null));

    options = options.Where(x => (x.Headers == null || x.Headers.Count == 0) || (x.Headers != null && HeaderExists(x.Headers, option.Headers)));

    return options.SingleOrDefault();
}

If we look at this equivalent version of your code, the error is in the options = options.Where(x => (x.HttpContent == option.HttpContent || x.HttpContent == null)); line.

I reproduced the issue by slightly changing the test. You can check it here: Kralizek@339ae33

Better AutoFixture support?

Hi!

AutoFixture is a great library for quickly building tests by assemblying test data.

One of the paradigms they support is the usage of test parameters as a way to receive the different parts of the test.

Here is an example

public class SomeHttpService
{
    public SomeHttpService(HttpClient client) { }

    public Task DoSomethingAsync() { }
}

[TestFixture]
public class SomeHttpServiceTests
{
    [Test, MyAutoData]
    public void A_test([Frozen] FakeHttpMessageHandler handler, SomeHttpService sut)
    {
        // customize options
        handler.Options.Add(new ...);

        sut.DoSomethingAsync();

        // verify sent request
        Assert.That(handler.Options[0].RequestUri, Is.EqualTo(something)
    }
}

Basically with this pattern, there is no direct access to the constructor and the FakeHttpMessageHandler can only be customized when it gets constructed.

Ability to specify expected headers

I have a scenario where i am (optionally) passing a header (in my case, the Authorization header) to a HTTP call.

It would be good to ensure that the call was made with this specific header.

e.g


var httpOptions = new HttpMessageOptions
{
   HttpMethod = HttpMethod.Post,
   RequestUri = "https://api.com/login",
   HttpResponseMessage = new HttpResponseMessage(HttpStatusCode.Created),
   Headers = new Dictionary<string,string>
   {
      {"Authorization", "Bearer xyz"}
   }
};

Then in the assertion, NumberOfTimesCalled should only be incremented for that request if that actual header was passed in HTTP call.

Error occurs when URL contains URL encoded values.

Try a URL like this:

http://www.whatever.com/something?json=%7B%0A%20%20%20%22Ids%22%3A%20%5B16036%2C1%5D%0A%7D

The CreateDictionaryKey method is erroring, presumably because of the string.Format not liking the braces...

Package fails with .NET Core 1.1

Restoring packages for
Package WorldDomination.HttpClient.Helpers 5.1.0 is not compatible with netcoreapp1.1 (.NETCoreApp,Version=v1.1). Package WorldDomination.HttpClient.Helpers 5.1.0 supports: net45 (.NETFramework,Version=v4.5)
Package Microsoft.Net.Http 2.2.29 is not compatible with netcoreapp1.1 (.NETCoreApp,Version=v1.1). Package Microsoft.Net.Http 2.2.29 supports:

  • monoandroid (MonoAndroid,Version=v0.0)
  • monotouch (MonoTouch,Version=v0.0)
  • net40 (.NETFramework,Version=v4.0)
  • net45 (.NETFramework,Version=v4.5)
  • portable-net40+sl4+win8+wp71+wpa81 (.NETPortable,Version=v0.0,Profile=net40+sl4+win8+wp71+wpa81)
  • portable-net45+win8 (.NETPortable,Version=v0.0,Profile=Profile7)
  • portable-net45+win8+wpa81 (.NETPortable,Version=v0.0,Profile=Profile111)
  • sl4-wp71 (Silverlight,Version=v4.0,Profile=WindowsPhone71)
  • win8 (Windows,Version=v8.0)
  • wpa81 (WindowsPhoneApp,Version=v8.1)
  • xamarinios10 (Xamarin.iOS,Version=v1.0)
    Package Microsoft.Bcl 1.1.10 is not compatible with netcoreapp1.1 (.NETCoreApp,Version=v1.1). Package Microsoft.Bcl 1.1.10 supports:
  • monoandroid (MonoAndroid,Version=v0.0)
  • monotouch (MonoTouch,Version=v0.0)
  • net40 (.NETFramework,Version=v4.0)
  • net45 (.NETFramework,Version=v4.5)
  • portable-net40+sl4+win8 (.NETPortable,Version=v0.0,Profile=net40+sl4+win8)
  • portable-net40+sl4+win8+wp71+wpa81 (.NETPortable,Version=v0.0,Profile=net40+sl4+win8+wp71+wpa81)
  • portable-net40+sl4+win8+wp8+wpa81 (.NETPortable,Version=v0.0,Profile=net40+sl4+win8+wp8+wpa81)
  • portable-net40+sl5+win8+wp8+wpa81 (.NETPortable,Version=v0.0,Profile=Profile328)
  • portable-net40+win8 (.NETPortable,Version=v0.0,Profile=Profile5)
  • portable-net40+win8+wp8+wpa81 (.NETPortable,Version=v0.0,Profile=net40+win8+wp8+wpa81)
  • portable-net45+win8+wp8+wpa81 (.NETPortable,Version=v0.0,Profile=Profile259)
  • portable-net45+win8+wpa81 (.NETPortable,Version=v0.0,Profile=Profile111)
  • portable-net451+win81 (.NETPortable,Version=v0.0,Profile=Profile44)
  • portable-net451+win81+wpa81 (.NETPortable,Version=v0.0,Profile=Profile151)
  • portable-win81+wp81+wpa81 (.NETPortable,Version=v0.0,Profile=Profile157)
  • sl4 (Silverlight,Version=v4.0)
  • sl4-wp71 (Silverlight,Version=v4.0,Profile=WindowsPhone71)
  • sl5 (Silverlight,Version=v5.0)
  • win8 (Windows,Version=v8.0)
  • wp8 (WindowsPhone,Version=v8.0)
  • wpa81 (WindowsPhoneApp,Version=v8.1)
  • xamarinios10 (Xamarin.iOS,Version=v1.0)
    One or more packages are incompatible with .NETCoreApp,Version=v1.1.
    Package restore failed. Rolling back package changes for 'API.Tests'.
    Time Elapsed: 00:00:00.8956040
    ========== Finished ==========

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.