'Integration test webapi core Authorization fiter

I would like to write and integration test for ASP.NET Core 6 Web API for Authorisation filter.

I would like to write integration test to validate authorization filter. any advice

I am getting an error

testhost.deps.json'. This file is required for functional tests to run properly. There should be a copy of the file on your source project bin folder. If that is not the case, make sure that the property PreserveCompilationContext is set to true on your project file. E.g 'true'. For functional tests to work they need to either run from the build output folder or the testhost.deps.json file from your application's output directory must be copied to the folder where the tests are running on. A common cause for this error is having shadow copying enabled when the tests run.

Code:

public class IntegrationTest
{
    protected readonly HttpClient _httpClient;

    protected IntegrationTest()
    {
        var appFactory = new WebApplicationFactory<Program>();
        _httpClient = appFactory.CreateClient();
    }

    protected async Task AuthenticateAsync()
    {
        _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("barer", await GetJetAsync());
    }

    private async Task<string> GetJetAsync()
    {
        string token = "eOCwiZXhwIjoxNjUyMzE1NzI4LCJhenAiOiJmN3B2OHFtUmoyaXFHS0xQbW1KYkIzdmNTQWN1UjlsZiIsInNjb3BlIjoib3BlbmlkIHByb2ZpbGUgZW1haWwgYWRkcmVzcyBwaG9uZSByZWFkOnByb2ZpbGUgcmVhZDpwcmVmZXJlbmNlcyB1cGRhdGU6cHJvZmlsZSB1cGRhdGU6cHJlZmVyZW5jZXMgdXBkYXRlOnByb2R1Y3QtbGlzdCByZWFkOnByb2R1Y3QtbGlzdCB1cGRhdGU6bG95YWx0eS1hY2NvdW50IHJlYWQ6bG95YWx0eS1hY2NvdW50IHJlYWQ6Y29sIHVwZGF0ZTpjb2wgc3NvOmNvbCBvZmZsaW5lX2FjY2VzcyIsImd0eSI6InBhc3N3b3JkIn0.blVb5UFO4sU0k3hptbbwXBk6RWGSr60wrsFlQTNbY76XyuqQgMNfU2SHHRq7qnaaC3UdMeYk4HJctfQDH8J3_itth2tF0FopHf_dUnxSnwq9Ga1dw4FXlscTwBUVEvzg_Q3VnhNL7Q8XWyfSthX4laatJ6_6X6cbGM6mdMNl5-8iSp3KH3GUbD3oOLZDKYylFJCxM2227FpPcdH-vI9I54Bqdtvs-2LPvClu-nsUEjo8no87G_llsjBEsEHmQO31Svq8h_8peumS_gpqOOwM6lAhulQcZOnd3m7-LfgGoXKLbJdXuw1rgEWbb-bD8bm7_dJ3i0Q99dkagY4RW6SHaQ";
        return await Task.Run(() => token);
    }

    [Fact]
    public async Task GetAll_ReturnsOK()
    {
        await AuthenticateAsync();
        string uri = "https://localhost:9999/products";
        var response = await _httpClient.GetAsync(uri);
        response.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source