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

[QUESTION] Regarding integration testing #1196

Open
SuperJMN opened this issue Jul 14, 2021 · 1 comment
Open

[QUESTION] Regarding integration testing #1196

SuperJMN opened this issue Jul 14, 2021 · 1 comment
Labels

Comments

@SuperJMN
Copy link

SuperJMN commented Jul 14, 2021

I would like to create integration tests. The real application is an ASP.NET API, but within the context of integration testing, I don't have access to the client registration goodness that is provided via Refit.HttpClientFactory.

So, I came up with this:

public class IntegrationTests
{
	public Task Test()
	{
		var sut = CreateSut();
		await sut.SomeMethod();
		// Rest of the test
	}

	private IMyApi CreateSut()
	{
		return RestService.For<IMyApi>(
			RestService.CreateHttpClient("http:/someserver.com/api",
				new RefitSettings()
				{
					AuthorizationHeaderValueGetter = () => GetToken(config.Authorization)
				}));
	}

	private async Task<string> GetToken(AuthorizationOptions authorizationOptions)
	{
		var client = RestService.CreateHttpClient(authorizationOptions.TokenUri.ToString(), new RefitSettings());
		var passwordTokenRequest = AuthorizationMixin.ToPasswordTokenRequest(authorizationOptions);
		var response = await client.RequestPasswordTokenAsync(passwordTokenRequest);
		var token = response.AccessToken;

		if (token is null)
		{
			throw new InvalidOperationException("Response token is null");
		}

		return token;
	}
}

So, what's the problem I see? The problem is that, in order to get the authorization token in `GetToken', I need to instantiate an a secondary HttpClient.

I guess if there's a better method to do this.

Thanks in advance!

@SuperJMN SuperJMN added the bug label Jul 14, 2021
@lostincomputer
Copy link

What does the code for getting a token have to do with HttpClientFactory?

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

No branches or pull requests

2 participants