예제 #1
0
        public void WhenIPerformAnHTTPRequestWithContent_Length(string httpTransactionType, string addContentLength)
        {
            HTTPBased http = new HTTPBased();

            switch (httpTransactionType.ToLower())
            {
            case "post":
                this.context["Response"] = http.Http(
                    HTTPBased.HTTPMethods.Post,
                    this.context.Keys.Any(key => key == "Domain") ? (string)this.context["Domain"] : null,
                    this.context.Keys.Any(key => key == "ResourcePath") ? (string)this.context["ResourcePath"] : null,
                    this.context.Keys.Any(key => key == "Query") ? (string)this.context["Query"] : null,
                    this.context.Keys.Any(key => key == "Header") ? (string)this.context["Header"] : null,
                    this.context.Keys.Any(key => key == "Payload") ? (string)this.context["Payload"] : null,
                    addContentLength.Trim().ToLower() == "added" ? true : false);
                break;

            case "get":
                this.context["Response"] = http.Http(
                    HTTPBased.HTTPMethods.Get,
                    this.context.Keys.Any(key => key == "Domain") ? (string)this.context["Domain"] : null,
                    this.context.Keys.Any(key => key == "ResourcePath") ? (string)this.context["ResourcePath"] : null,
                    this.context.Keys.Any(key => key == "Query") ? (string)this.context["Query"] : null,
                    this.context.Keys.Any(key => key == "Header") ? (string)this.context["Header"] : null,
                    this.context.Keys.Any(key => key == "Payload") ? (string)this.context["Payload"] : null,
                    addContentLength.Trim().ToLower() == "added" ? true : false);
                break;

            default:
                throw new ArgumentException($"Only supporting POST and GET.  Got [{httpTransactionType}]", "httpTransactionType");
            }
        }
예제 #2
0
        public void WhenISetupAndPerformAnHTTPT(string httpTransactionType)
        {
            HTTPBased http = new HTTPBased();

            http.Domain       = (string)this.context["Domain"];
            http.ResourcePath = (string)this.context["ResourcePath"];

            if (this.context.ContainsKey("Query"))
            {
                if (this.context["Query"] is HTTPBased.ItemList)
                {
                    http.SetQueryStringFromItemList((HTTPBased.ItemList) this.context["Query"]);
                }
                else
                {
                    http.QueryString = (string)this.context["Query"];
                }
            }

            if (this.context.ContainsKey("Header"))
            {
                if (this.context["Header"] is HTTPBased.ItemList)
                {
                    http.SetHeaderStringFromItemList((HTTPBased.ItemList) this.context["Header"]);
                }
                else
                {
                    http.HeaderString = (string)this.context["Header"];
                }
            }

            http.Body = this.context.ContainsKey("Payload") ? (string)this.context["Payload"] : null;

            switch (httpTransactionType.ToLower())
            {
            case "post":
                http.HTTPMethod          = HTTPBased.HTTPMethods.Post;
                this.context["Response"] = http.Http();
                break;

            case "get":
                http.HTTPMethod          = HTTPBased.HTTPMethods.Get;
                this.context["Response"] = http.Http();
                break;

            default:
                throw new ArgumentException($"Only supporting POST and GET.  Got [{httpTransactionType}]", "httpTransactionType");
            }
        }
        public static void Example_AcceptSSLCertificateUsingCustomDelegate()
        {
            // We use the Postman example website for this as the server is well setup for examples.
            string url = "postman-echo.com/post";

            // To prevent Log from output messages to Console we catch them and tell Log to not output to console.
            Log.LogOutputDelegate = LogDelegate;
            Log.LogToConsole      = false;

            // Ensure Repository has been cleared - incase any examples have changed anything...
            Repository.ClearRepositoryAll();

            // Instantiate HTTPBased and set the domain and resoure path of the URL.
            HTTPBased httpRequest = new HTTPBased();

            httpRequest.HTTPMethod   = HTTPBased.HTTPMethods.Post;
            httpRequest.Domain       = url.Split('/')[0];
            httpRequest.ResourcePath = "/" + url.Split('/')[1];
            httpRequest.CertificateValidationCallback = AcceptHTTPSCertificate;  // This tells HTTPBased to use the AcceptHTTPSCertificate method to make certificate acceptance decision.

            // Setup the HTTP Header items.  Header should be well-formed so we use the HTTPBased.ItemList to set the items as that will be well-formed.
            // NOTE.  We have NOT set the Connection: close" and so
            HTTPBased.ItemList headerItems = new HTTPBased.ItemList();
            headerItems.Add("accept", "*/*");
            headerItems.Add("content-type", "text/plain");
            headerItems.Add("host", httpRequest.Domain);
            headerItems.Add("accept-encoding", "identity");
            headerItems.Add("connection", "close");
            httpRequest.SetHeaderStringFromItemList(headerItems);

            httpRequest.UseSSL = true;

            httpRequest.Body = "Hello";

            try
            {
                HTTPBased.ItemList response = httpRequest.Http();

                OutputDetails("Example of a simple HTTP POST", httpRequest, response,
                              "Property .UseSSL is set true and .CertificateValidationCallback is set",
                              "to 'AcceptHTTPSCertificate' method.  Method receives callback with",
                              "certificate details:-",
                              $"{serverCertificate})",
                              "which we just accept in this case.  A test may want to validate the certificate",
                              "or just log the details etc...");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
            }
        }
        public static void Example_HTTPSPostAutoAcceptSSLCertificate()
        {
            // We use the Postman example website for this as the server is well setup for examples.
            string url = "postman-echo.com/post";

            // To prevent Log from output messages to Console we catch them and tell Log to not output to console.
            Log.LogOutputDelegate = LogDelegate;
            Log.LogToConsole      = false;

            // Ensure Repository has been cleared - incase any examples have changed anything...
            Repository.ClearRepositoryAll();

            // Tell NonGUI test library to accept the Server Certificate in an SSL (HTTPS) call
            Repository.ItemLocal["TeamControlium.NonGUI", "SSL_AcceptServerCertificate"] = true;

            // Instantiate HTTPBased and set the domain and resoure path of the URL.
            HTTPBased httpRequest = new HTTPBased();

            httpRequest.HTTPMethod   = HTTPBased.HTTPMethods.Post;
            httpRequest.Domain       = url.Split('/')[0];
            httpRequest.ResourcePath = "/" + url.Split('/')[1];

            // Setup the HTTP Header items.  Header should be well-formed so we use the HTTPBased.ItemList to set the items as that will be well-formed.
            // NOTE.  We have NOT set the Connection: close" and so
            HTTPBased.ItemList headerItems = new HTTPBased.ItemList();
            headerItems.Add("accept", "*/*");
            headerItems.Add("content-type", "text/plain");
            headerItems.Add("host", httpRequest.Domain);
            headerItems.Add("accept-encoding", "identity");
            headerItems.Add("connection", "close");
            httpRequest.SetHeaderStringFromItemList(headerItems);

            httpRequest.UseSSL = true;

            httpRequest.Body = "Hello";

            try
            {
                HTTPBased.ItemList response = httpRequest.Http();

                OutputDetails("Example of a simple HTTP POST", httpRequest, response,
                              $"Domain {httpRequest.Domain} exists and just echo's the post request so we",
                              "expect the response to contain our requests body in the JSON 'data' field", "",
                              $"Note how the header line 'Content-Length: {httpRequest.Body.Length}' has been",
                              "automatically added to the Request header by HTTPBased.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
            }
        }
        public static void Example_SimpleGetUsingProperties()
        {
            // Set an existant domain and resource path.
            string url = "postman-echo.com/get";

            // To prevent Log from output messages to Console we catch them and tell Log to not output to console.
            Log.LogOutputDelegate = LogDelegate;
            Log.LogToConsole      = false;

            // Ensure Repository has been cleared - incase any examples have changed anything...
            Repository.ClearRepositoryAll();

            // Instantiate HTTPBased and set the domain and resoure path of the URL.
            // So this loads httpRequest.Domain with postman-echo.com
            // and httpRequest.ResourcePath with /post
            HTTPBased httpRequest = new HTTPBased();

            httpRequest.HTTPMethod   = HTTPBased.HTTPMethods.Get;
            httpRequest.Domain       = url.Split('/')[0];
            httpRequest.ResourcePath = "/" + url.Split('/')[1];

            HTTPBased.ItemList queryArguments = new HTTPBased.ItemList();
            queryArguments.Add("foo", "bar");
            queryArguments.Add("alpha", "7");
            httpRequest.SetQueryStringFromItemList(queryArguments);

            // Setup the HTTP Header items.  We could use a list and call SetHeaderStringFromItemList.  But for sake of example we will build the string
            // ourselves.
            httpRequest.HeaderString = $"content-type: text/plain\r\naccept: */*\r\nhost: {httpRequest.Domain}\r\naccept-encoding: identity\r\nconnection: close\r\n";


            // Perform HTTP Post.  Use TryHttpPOST as we are expecting an error.  In a Test script, using the Try... would be preferential anyway incase a defect is touched and
            // the POST fails badly.  If an HTTP POST endpoint ouside the test domain is being used (IE. A Mock) then standard HttpPOST could be used.

            try
            {
                HTTPBased.ItemList response = httpRequest.Http();

                OutputDetails("Example of a simple HTTP GET", httpRequest, response,
                              $"Domain {httpRequest.Domain} exists and just echo's the get request. So we",
                              "expect the response to contain our requests arguments (foo=bar and alpha=7) in the",
                              "JSON 'args' field", "",
                              $"Note how the header line 'Content-Length: {httpRequest.Body.Length}' has been",
                              "automatically added to the Request header by HTTPBased.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
            }
        }