Пример #1
0
 public Request(HttpMethod method, string url, IDictionary <string, string> headers, RequestPayload body)
 {
     Method  = method;
     Url     = url;
     Headers = headers;
     Body    = body;
 }
		public Request(HttpMethod method, string url, IDictionary<string, string> headers, RequestPayload body)
		{
			Method = method;
			Url = url;
			Headers = headers;
			Body = body;
		}
		private string GetAuthorizationHeader(RequestData requestData, string fullUrl, ApiRequest apiRequest, RequestPayload requestBody)
		{
			if (requestData.RequiresSignature)
			{
				return BuildOAuthHeader(requestData, fullUrl, apiRequest.Parameters, requestBody);
			}

			return "oauth_consumer_key=" + _oAuthCredentials.ConsumerKey;
		}
		private static ApiWebException MakeOriginalException()
		{
			var innerException = new WebException("test");
			var requestBody = new RequestPayload("foo", "bar");
			var headers = new Dictionary<string, string> {{"key1", "value1"}};
			var originalRequest = new Request(HttpMethod.Get, "http://foo.com/bar?foo=bar",
				headers, requestBody);

			var inputException = new ApiWebException("request failed", innerException, originalRequest);
			return inputException;
		}
		public async void bad_url_post__should_return_not_found()
		{
			var url = string.Format("{0}/foo/bar/fish/1234?oauth_consumer_key={1}", API_URL, _consumerKey);
			var parameters = new Dictionary<string, string>
			{
				{"foo", "bar"}
			};

			var queryString = parameters.ToQueryString();
			var requestPayload = new RequestPayload("application/xml", queryString);
			var request = new Request(HttpMethod.Post, url, new Dictionary<string, string>(), requestPayload, null);

			var response = await new HttpClientMediator().Send(request);
			AssertResponse(response, HttpStatusCode.NotFound);
		}
		public async void Should_populate_OriginalRequest_property_with_the_request_passed_to_Send_method()
		{
			var url = string.Format("{0}/foo/bar/fish/1234?oauth_consumer_key={1}", API_URL, _consumerKey);
			var parameters = new Dictionary<string, string>
			{
				{"foo", "bar"}
			};

			var queryString = parameters.ToQueryString();
			var requestPayload = new RequestPayload("application/xml", queryString);
			var expectedHeaders = new Dictionary<string, string>
			{
				{"headerKey", "headerValue"}
			};
			const string expectedTraceId = "CUSTOM_TRACE_ID";
			var originalRequest = new Request(HttpMethod.Post, url, expectedHeaders, requestPayload, expectedTraceId);

			var response = await new HttpClientMediator().Send(originalRequest);

			Assert.That(response.OriginalRequest.Url, Is.EqualTo(url));
			Assert.That(response.OriginalRequest.Headers, Is.EqualTo(expectedHeaders));
			Assert.That(response.OriginalRequest.Body, Is.EqualTo(requestPayload));
			Assert.That(response.OriginalRequest.Method, Is.EqualTo(HttpMethod.Post));
			Assert.That(response.OriginalRequest.TraceId, Is.EqualTo(expectedTraceId));
		}
		private static bool ShouldReadParamsFromBody(IDictionary<string, string> parameters, RequestPayload requestBody)
		{
			return (requestBody.ContentType == FormUrlEncoded) && 
				!string.IsNullOrEmpty(requestBody.Data) && 
				(parameters.Count == 0);
		}
		private string BuildOAuthHeader(RequestData requestData, string fullUrl, IDictionary<string, string> parameters, RequestPayload requestBody)
		{
			var httpMethod = requestData.HttpMethod.ToString().ToUpperInvariant();

			var oauthRequest = new OAuthRequest
				{
					Type = OAuthRequestType.ProtectedResource,
					RequestUrl = fullUrl,
					Method = httpMethod,
					ConsumerKey = _oAuthCredentials.ConsumerKey,
					ConsumerSecret = _oAuthCredentials.ConsumerSecret,
				};

			if (!string.IsNullOrEmpty(requestData.OAuthToken))
			{
				oauthRequest.Token = requestData.OAuthToken;
				oauthRequest.TokenSecret = requestData.OAuthTokenSecret;
			}

			if (ShouldReadParamsFromBody(parameters, requestBody))
			{
				var bodyParams = HttpUtility.ParseQueryString(requestBody.Data);
				var keys = bodyParams.AllKeys.Where(x => !string.IsNullOrEmpty(x));
				foreach (var key in keys)
				{
					parameters.Add(key, bodyParams[key]);
				}
			}

			return oauthRequest.GetAuthorizationHeader(parameters);
		}