How to perform a 302 Redirect with a forwarding URL

457 Views Asked by At

We are using Xamarin and UITest in order to create UI tests for our application.

We are using Azure API management for integration, and am trying to mock our Authentication process. We are using Xamarin Essentials WebAuthenticator, which opens an external browser in order to do the two step OAuth and load a login page.

Since UITest cannot control anything outside of the app itself, we need to have API management respond by giving a 302 redirect back to the controlling app, with our test authentication tokens that are mocked.

I've tried creating a mock response that returns a 302, with a Location header with the value of our return url for the app. However whenever I test this in the APIM test page, it simply just returns a 200 OK.

Is it possible to make a GET request to APIM, which performs a 302 redirect to a specified uri, with a body of mocked test data?

1

There are 1 best solutions below

0
On

You may try with a return-response policy. Something similar to this example:

<when condition="@(context.Request.OriginalUrl.Scheme == Uri.UriSchemeHttp)">
            <return-response>
                <set-status code="302" reason="Temporary Redirect" />
                <set-header name="Location" exists-action="override">
                    <value>@{
                    var uri = context.Request.OriginalUrl;

                    return Uri.UriSchemeHttps + "://" + uri.Host + uri.Path + uri.QueryString;
                    }</value>
                </set-header>
            </return-response>
        </when>

For your reference: https://learn.microsoft.com/en-us/azure/api-management/api-management-advanced-policies#ReturnResponse