This is a migrated thread and some comments may be shown as answers.

Need help with HttpProxy to intercept Request headers

3 Answers 165 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
ram
Top achievements
Rank 1
ram asked on 04 Apr 2018, 03:01 AM

I have a scenario wherein I need to provide Authorization credentials before navigating to a webpage.
After going through the documentation provided here https://docs.telerik.com/teststudio/testing-framework/write-tests-in-code/advanced-topics-wtc/using-the-http-proxy I thought of using HttpProxy to intercept the request and inject "Authorization" header in the Request pipeline.
For some reason I am not able to see the HTTP headers that I am trying to inject. Am I doing something wrong here?

My function looks like this
public void NavigateWithCredentils(Uri navigateUrl)
{
RequestListenerInfo li = new RequestListenerInfo(AddCredentials);
Telerik.Manager.Http.AddBeforeRequestListener(li);
Telerik.ActiveBrowser.NavigateTo(navigateUrl);
Telerik.Manager.Http.RemoveBeforeRequestListener(li);
}

private void AddCredentials(object sender, HttpRequestEventArgs e)
{
if (e.Request.Headers.GetValues("Authorization") == null)
{
e.Request.Headers.Add("Authorization", "Basic realusername:realpassword");
}
}

Note: UseHttpProxy is already set to true in the Initialization code.

3 Answers, 1 is accepted

Sort by
0
Elena
Telerik team
answered on 06 Apr 2018, 11:06 AM
Hi Ram,

Thank you for your interest. 

As of the shared details I suspect that the required scenario could not be fully accomplished with the http proxy. Since you would like to have the authorization for a request I would recommend you give a try to the Test Studio for APIs. Since it is still in Beta stage it is a free product and allows you to easily intercept http requests. 

In case the above is not quite relevant to your query please elaborate more on the whole scenario to have overview for the requirements. 

Thank you in advance! 

Regards,
Elena Tsvetkova
Progress Telerik
 
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
 
0
ram
Top achievements
Rank 1
answered on 08 Apr 2018, 03:17 PM

Hi Elena,

Thanks for suggesting TestStudio for API but I am looking for a programmatic way of Authorizing my HTTP request in my existing Test suite built using NUnit and artoftest DLLs.

-Ram

0
Ivaylo
Telerik team
answered on 11 Apr 2018, 03:20 PM
Hello Ram,

I'm providing you a sample on how this can be achieved. Please note that I am using Console Application and I am using the Console.ReadKey(); line for synchronization. You should figure out how to do the synchronization in NUnit.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ArtOfTest.WebAii.Messaging.Http;
using ArtOfTest.WebAii.Core;
 
namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {
 
            var settings = new Settings();
            settings.Web.UseHttpProxy = true;
            var manager = new Manager(settings);
            manager.Start();
            manager.Http.AddBeforeRequestListener(new RequestListenerInfo(new HttpRequestHandler(BeforeRequest)));
 
 
            manager.LaunchNewBrowser();
            manager.ActiveBrowser.NavigateTo("http://www.bing.com");
 
 
            Console.ReadKey();
            manager.Dispose();
        }
 
        private static void BeforeRequest(object sender, HttpRequestEventArgs r)
        {
            if (!r.Request.RequestUri.ToLower().Contains("bing"))
            {
                return;
            }
 
            var request = r.Request;
            request.Headers.Add("Authorization", "Lava");
        }
    }
}


Regards,
Ivaylo
Progress Telerik
 
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
 
Tags
General Discussions
Asked by
ram
Top achievements
Rank 1
Answers by
Elena
Telerik team
ram
Top achievements
Rank 1
Ivaylo
Telerik team
Share this question
or