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); } }
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"); } }