/// <summary> /// Queries an API endpoint asynchronously and returs its result. /// </summary> /// <param name="type">The type of Real (endpoint) to query.</param> /// <returns>A task that contains a normalized <see cref="RealResponse"/> object.</returns> public async Task <RealResponse> GetRealRate(RealEndpoints type) { RealResponse cachedResponse = Cache.GetObject <RealResponse>(type); if (cachedResponse != null) { return(cachedResponse); } else { string endpoint = type.GetDescription(); RestRequest request = new(endpoint); RestResponse <RealResponse> response = await Client.ExecuteGetAsync <RealResponse>(request); if (response.IsSuccessful) { RealResponse realResponse = response.Data; realResponse.Type = type; Cache.SaveObject(type, realResponse); return(realResponse); } else { OnError(response); return(null); } } }
/// <summary> /// Since these tests rely on live web requests - run as few /// as possible. This makes the test a little harder to read /// but should improve build speed / consistency. /// </summary> public GitHubHomePageRequestFixture() { // necessary for requests to github to work ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; RequestBody = "Hello World"; var requestUri = new Uri("http://www.github.com"); var requestCookie = new Cookie("test", "test") { Domain = requestUri.Host }; // first make a request using the real HttpWebRequest var realHttRequestCreator = (IWebRequestCreate) // use reflection in case HttpWebRequestWrapperSession has changed // the PrefixList Activator.CreateInstance( typeof(HttpWebRequest).Assembly.GetType("System.Net.HttpRequestCreator")); RealRequest = (HttpWebRequest)realHttRequestCreator.Create(requestUri); RealRequest.CookieContainer = new CookieContainer(); RealRequest.CookieContainer.Add(requestCookie); RealRequest.Method = "POST"; using (var sw = new StreamWriter(RealRequest.GetRequestStream())) sw.Write(RequestBody); RealResponse = (HttpWebResponse)RealRequest.GetResponse(); using (var sr = new StreamReader(RealResponse.GetResponseStream())) RealResponseBody = sr.ReadToEnd(); // then make the same request using the recorder RecorderRequest = new HttpWebRequestWrapperRecorder(requestUri) { CookieContainer = new CookieContainer() }; RecorderRequest.CookieContainer.Add(requestCookie); RecorderRequest.Method = "POST"; using (var sw = new StreamWriter(RecorderRequest.GetRequestStream())) sw.Write(RequestBody); RecorderResponse = (HttpWebResponse)RecorderRequest.GetResponse(); using (var sr = new StreamReader(RecorderResponse.GetResponseStream())) RecorderResponseBody = sr.ReadToEnd(); RecorderRecording = RecorderRequest.RecordedRequests.First(); }