Need help with HttpProxy to intercept Request headers

4 posts, 0 answers
  1. ram
    ram avatar
    2 posts
    Member since:
    Apr 2018

    Posted 03 Apr Link to this post

    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.

  2. Elena Tsvetkova
    Admin
    Elena Tsvetkova avatar
    934 posts

    Posted 06 Apr Link to this post

    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
     
  3. ram
    ram avatar
    2 posts
    Member since:
    Apr 2018

    Posted 08 Apr in reply to Elena Tsvetkova Link to this post

    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

  4. Ivaylo
    Admin
    Ivaylo avatar
    775 posts

    Posted 11 Apr Link to this post

    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
     
Back to Top