0

I am working on trying to test some code by making it a console app just to see if I get the proper response returned. I am having a hard time converting what I had into a console app. Would anyone know how I can go about testing this correctly?

public static void Main(string[] args)
    {
        string TrackingNumber1;
        string TrackingNumber2;
        string TrackingNumber3;

        Console.WriteLine("Enter First Tracking Number:");
        TrackingNumber1 = Console.ReadLine();

        Console.WriteLine("Enter Second Tracking Number:");
        TrackingNumber2 = Console.ReadLine();

        Console.WriteLine("Enter Third Tracking Number:");
        TrackingNumber3 = Console.ReadLine();
    }
    public class UspsService
    {

        //Base URL for USPS Address and Zip Code validation API
        private const string baseURL = "http://testurl.com";
        //Web client instance.
        private WebClient wsClient = new WebClient();
        //User ID obtained from USPS.
        public string usps_UserID = "";

        private string GetDataFromSite(string uspsRequest)
        {
            string strResponse = "";
            //Send the request to USPS.
            byte[] responseData = wsClient.DownloadData(uspsRequest);
            //Convert byte stream to string data.
            foreach (byte oItem in responseData)
                strResponse += (char)oItem;
            return strResponse;
        }


        //method that builds the track request, and sends returns the formatted string to send to USPS endpoint
        public string TrackRequest(string trackingNumber1, string trackingNumber2, string trackingNumber3)
        {
            string strResponse = "", strUSPS = "";
            strUSPS = baseURL + "?API=TrackV2&XML=<?xml version =\"1.0\" encoding =\"utf-8\"?>";
            strUSPS += "<TrackRequest USERID=\"" + usps_UserID + "\">";
            strUSPS += "<TrackID ID=\"" + trackingNumber1 + "\"> </TrackID>";
            strUSPS += "<TrackID ID=\"" + trackingNumber2 + "\"> </TrackID>";
            strUSPS += "<TrackID ID=\"" + trackingNumber3 + "\"> </TrackID>";
            strUSPS += "</TrackRequest>";
            //Send the request to USPS.
            strResponse = GetDataFromSite(strUSPS);
            Console.WriteLine(strResponse);
            return strResponse;

        }
  • What kind of project did you have? Why are you having a hard time? Are you having errors? Could elaborate a bit more on the steps you have/are taking? – MX D Dec 04 '18 at 16:29
  • basically i am trying to test to see if I have properly formed this request to send to the carrier. I wanted to put it in a console app so that it can return the data in an output window. I've tried printing the response directly from the method, and tried calling the method in the main but am somehow unable to –  Dec 04 '18 at 16:33
  • In this sample code, you are not calling the methods at all. Hence you will never get a response. Could you create a MCVE where you call the functions such as `TrackRequest`, and be a bit more specific with what does not work? Aka, you are not getting anything, or you are getting an error. Maybe that the console unexpectedly closes. – MX D Dec 04 '18 at 16:38
  • Nur sure if this is your question, but instead of writing console apps for manual testing, consider unit tests: You could send one request manually (e.g. using Postman). Now you know how a request and response should look like and you can easily write your tests. (xunit theory). – Christoph Lütjen Dec 04 '18 at 17:01
  • Btw.: You should use StringBuilder and escape tracking numbers in your XML or better: https://stackoverflow.com/questions/11492705/how-to-create-an-xml-document-using-xmldocument – Christoph Lütjen Dec 04 '18 at 17:04

1 Answers1

0

First, There are some predefined applications for Testing purposes Like PostMan, Browser Add-ones, ... And there are useful libraries also, RestSharp for example.

I would prefer to use predefined applications for testing and gathering, a manual for an existing api. they are specially designed to help you to find, what you need to send.

Second, After testing the API, you may need to map them into a specific language (C#, JavaScript, JQuery, ...). This step is pretty easy, It's just a conversion.

RezaNoei
  • 1,266
  • 1
  • 8
  • 24