/// <summary> /// Creates a new instance of <see cref="MockClientHttpResponse"/>. /// </summary> /// <param name="body">The body of the response as a stream.</param> /// <param name="headers">The response headers.</param> /// <param name="statusCode">The response status code.</param> /// <param name="statusDescription">The response status description.</param> public MockClientHttpResponse(Stream body, HttpHeaders headers, HttpStatusCode statusCode, string statusDescription) { this.body = body; this.headers = headers; this.statusCode = statusCode; this.statusDescription = statusDescription; }
public void Setup() { twitter = new TwitterTemplate("API_KEY", "API_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET"); mockServer = MockRestServiceServer.CreateServer(twitter.RestTemplate); responseHeaders = new HttpHeaders(); responseHeaders.ContentType = MediaType.APPLICATION_JSON; }
/// <summary> /// Creates a new instance of <see cref="HttpEntity"/> with the given body and headers. /// </summary> /// <param name="body">The entity body.</param> /// <param name="headers">The entity headers.</param> public HttpEntity(object body, HttpHeaders headers) { ArgumentUtils.AssertNotNull(headers, "headers"); this.body = body; this.headers = headers; }
public void Setup() { dropbox = new DropboxTemplate("CONSUMER_KEY", "CONSUMER_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET", AccessLevel.AppFolder); mockServer = MockRestServiceServer.CreateServer(dropbox.RestTemplate); responseHeaders = new HttpHeaders(); responseHeaders.ContentType = MediaType.APPLICATION_JSON; }
public void Setup() { linkedIn = new LinkedInTemplate("API_KEY", "API_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET"); mockServer = MockRestServiceServer.CreateServer(linkedIn.RestTemplate); responseHeaders = new HttpHeaders(); responseHeaders.ContentType = new MediaType("application", "json", Encoding.Default); }
public void FetchNewRequestToken_OAuth10a() { MockRestServiceServer mockServer = MockRestServiceServer.CreateServer(oauth10a.RestTemplate); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.ContentType = MediaType.APPLICATION_FORM_URLENCODED; mockServer.ExpectNewRequest() .AndExpectUri(REQUEST_TOKEN_URL) .AndExpectMethod(HttpMethod.POST) .AndExpectHeaderContains("Authorization", "oauth_callback=\"http%3A%2F%2Fwww.someclient.com%2Foauth%2Fcallback\"") .AndExpectHeaderContains("Authorization", "oauth_version=\"1.0\"") .AndExpectHeaderContains("Authorization", "oauth_signature_method=\"HMAC-SHA1\"") .AndExpectHeaderContains("Authorization", "oauth_consumer_key=\"consumer_key\"") .AndExpectHeaderContains("Authorization", "oauth_nonce=\"") .AndExpectHeaderContains("Authorization", "oauth_signature=\"") .AndExpectHeaderContains("Authorization", "oauth_timestamp=\"") .AndRespondWith(EmbeddedResource("RequestToken.formencoded"), responseHeaders); #if NET_4_0 || SILVERLIGHT_5 OAuthToken requestToken = oauth10a.FetchRequestTokenAsync("http://www.someclient.com/oauth/callback", null).Result; #else OAuthToken requestToken = oauth10a.FetchRequestToken("http://www.someclient.com/oauth/callback", null); #endif Assert.AreEqual("1234567890", requestToken.Value); Assert.AreEqual("abcdefghijklmnop", requestToken.Secret); }
/// <summary> /// Creates a new instance of <see cref="HttpResponseMessage"/> with the given headers, status code and status description. /// </summary> /// <param name="headers">The response headers.</param> /// <param name="statusCode">The HTTP status code.</param> /// <param name="statusDescription">The HTTP status description.</param> public HttpResponseMessage(HttpHeaders headers, HttpStatusCode statusCode, string statusDescription) { ArgumentUtils.AssertNotNull(headers, "headers"); this.headers = headers; this.statusCode = statusCode; this.statusDescription = statusDescription; }
public void Setup() { restBucksClient = new RestBucksClient(); mockServer = MockRestServiceServer.CreateServer(this.restBucksClient.RestTemplate); responseHeaders = new HttpHeaders(); responseHeaders.ContentType = new MediaType("application", "vnd.restbucks+xml"); }
/// <summary> /// Creates a <see cref="ResponseCreator"/> that respond with /// the given response body, headers, status code, and status description. /// </summary> /// <param name="body">The body of the response.</param> /// <param name="headers">The response headers.</param> /// <param name="statusCode">The response status code.</param> /// <param name="statusDescription">The response status description.</param> /// <returns>A <see cref="ResponseCreator"/>.</returns> public static ResponseCreator CreateWith( string body, HttpHeaders headers, HttpStatusCode statusCode, string statusDescription) { ArgumentUtils.AssertNotNull(body, "body"); return delegate(IClientHttpRequest request) { return new MockClientHttpResponse(new MemoryStream(Encoding.UTF8.GetBytes(body)), headers, statusCode, statusDescription); }; }
/// <summary> /// Creates a <see cref="ResponseCreator"/> that respond with /// the given response body (from a <see cref="IResource"/>), headers, status code, and status description. /// </summary> /// <param name="body">The <see cref="IResource"/> containing the body of the response.</param> /// <param name="headers">The response headers.</param> /// <param name="statusCode">The response status code.</param> /// <param name="statusDescription">The response status description.</param> /// <returns>A <see cref="ResponseCreator"/>.</returns> public static ResponseCreator CreateWith( IResource body, HttpHeaders headers, HttpStatusCode statusCode, string statusDescription) { ArgumentUtils.AssertNotNull(body, "body"); return delegate(IClientHttpRequest request) { return new MockClientHttpResponse(body.GetStream(), headers, statusCode, statusDescription); }; }
public void Setup() { twitter = new TwitterTemplate("API_KEY", "API_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET"); mockServer = MockRestServiceServer.CreateServer(twitter.RestTemplate); responseHeaders = new HttpHeaders(); responseHeaders.ContentType = MediaType.APPLICATION_JSON; unauthorizedTwitter = new TwitterTemplate(); // TODO: create a mock server just to avoid hitting real twitter if something gets past the authorization check //MockRestServiceServer.CreateServer(unauthorizedTwitter.RestTemplate); }
public void Setup() { intuit = new IntuitTemplate("API_KEY", "API_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET"); mockServer = MockRestServiceServer.CreateServer(intuit.RestTemplate); responseHeaders = new HttpHeaders(); responseHeaders.ContentType = MediaType.TEXT_XML; mockServer.ExpectNewRequest() .AndExpectUri("https://services.intuit.com/sb/company/v2/availableList") .AndExpectMethod(HttpMethod.GET) .AndRespondWith(xmlResource("intuit-companylist"), responseHeaders); }
// Builds an authorization header for a request. // Expects that the request's query parameters are form-encoded. public string BuildAuthorizationHeaderValue(Uri requestUri, HttpMethod requestMethod, HttpHeaders requestHeaders, Action<Stream> requestBody, string consumerKey, string consumerSecret, string accessToken, string accessTokenSecret) { IDictionary<string, string> oauthParameters = CreateCommonOAuthParameters(consumerKey); oauthParameters.Add("oauth_token", accessToken); NameValueCollection additionalParameters = ReadFormParameters(requestHeaders.ContentType, requestBody); NameValueCollection queryParameters = ParseFormParameters(requestUri.Query.TrimStart('?')); foreach (string queryParameterName in queryParameters) { additionalParameters.Add(queryParameterName, queryParameters[queryParameterName]); } return this.BuildAuthorizationHeaderValue(requestMethod, requestUri, oauthParameters, additionalParameters, consumerSecret, accessTokenSecret); }
public void BuildAuthorizationHeaderValue_Request() { SigningSupport signingUtils = new SigningSupport(); signingUtils.TimestampGenerator = new MockTimestampGenerator(123456789, 987654321); Uri uri = new Uri(String.Format("http://example.com/request?b5={0}&a3=a&{1}=&a2={2}", Uri.EscapeDataString("=%3D"), Uri.EscapeDataString("c@"), Uri.EscapeDataString("r b"))); HttpHeaders headers = new HttpHeaders(); headers.ContentType = MediaType.APPLICATION_FORM_URLENCODED; string authorizationHeader = signingUtils.BuildAuthorizationHeaderValue(uri, HttpMethod.POST, headers, stream => { byte[] byteData = Encoding.UTF8.GetBytes("c2&a3=2+q"); stream.Write(byteData, 0, byteData.Length); }, "9djdj82h48djs9d2", "consumer_secret", "kkk9d7dh3k39sjv7", "token_secret"); AssertAuthorizationHeader(authorizationHeader, "%2B8iwuQbJ%2Fa46KYDAFQlzPerVGYk%3D"); }
public void BinarySerialization() { byte[] body = new byte[2] { 0, 1 }; HttpHeaders headers = new HttpHeaders(); headers.ContentType = new MediaType("text", "plain"); HttpStatusCode statusCode = HttpStatusCode.Accepted; string statusDescription = "Accepted description"; HttpResponseException exBefore = new HttpResponseException( new HttpResponseMessage<byte[]>(body, headers, statusCode, statusDescription)); HttpResponseException exAfter = SerializationTestUtils.BinarySerializeAndDeserialize(exBefore) as HttpResponseException; Assert.IsNotNull(exAfter); Assert.AreEqual(body, exAfter.Response.Body, "Invalid response body"); Assert.AreEqual(new MediaType("text", "plain"), exAfter.Response.Headers.ContentType, "Invalid response headers"); Assert.AreEqual(statusCode, exAfter.Response.StatusCode, "Invalid status code"); Assert.AreEqual(statusDescription, exAfter.Response.StatusDescription, "Invalid status description"); }
public void BinarySerialization() { Uri requestUri = new Uri("http://www.springframework.net"); HttpMethod requestMethod = HttpMethod.POST; byte[] body = new byte[2] { 0, 1 }; HttpHeaders headers = new HttpHeaders(); headers.ContentType = new MediaType("text", "plain"); HttpStatusCode statusCode = HttpStatusCode.Accepted; string statusDescription = "Accepted description"; HttpResponseException exBefore = new HttpResponseException(requestUri, requestMethod, new HttpResponseMessage<byte[]>(body, headers, statusCode, statusDescription)); HttpResponseException exAfter = SerializationTestUtils.BinarySerializeAndDeserialize(exBefore) as HttpResponseException; Assert.IsNotNull(exAfter); Assert.AreEqual(requestUri, exAfter.RequestUri, "Invalid request URI"); Assert.AreEqual(requestMethod, exAfter.RequestMethod, "Invalid request method"); Assert.AreEqual(body, exAfter.Response.Body, "Invalid response body"); Assert.AreEqual(new MediaType("text", "plain"), exAfter.Response.Headers.ContentType, "Invalid response headers"); Assert.AreEqual(statusCode, exAfter.Response.StatusCode, "Invalid status code"); Assert.AreEqual(statusDescription, exAfter.Response.StatusDescription, "Invalid status description"); }
void PrepareMocks() { _restTemplate = new RestTemplate(BaseUrl); _restTemplate.MessageConverters.Add(new NJsonHttpMessageConverter()); _mockRestServer = MockRestServiceServer.CreateServer(_restTemplate); _responseHeaders = new HttpHeaders {ContentType = new MediaType("application", "json")}; ClientSettings.Instance.ServerUrl = BaseUrl; _restRequestCreator = new RestRequestCreator(); _restRequestCreator.SetRestTemplate(_restTemplate); RestRequestFactory.RestRequestCreator = _restRequestCreator; }
/// <summary> /// Responds with the given response body (from a <see cref="IResource"/>), headers, status code, and status description. /// </summary> /// <param name="requestActions">The <see cref="IRequestActions"/> to set up response on.</param> /// <param name="body">The <see cref="IResource"/> containing the body of the response.</param> /// <param name="headers">The response headers.</param> /// <param name="statusCode">The response status code.</param> /// <param name="statusDescription">The response status description.</param> public static void AndRespondWith(this IRequestActions requestActions, IResource body, HttpHeaders headers, HttpStatusCode statusCode, string statusDescription) { requestActions.AndRespond(ResponseCreators.CreateWith(body, headers, statusCode, statusDescription)); }
/// <summary> /// Responds with the given response body (from a <see cref="IResource"/>) and headers. The response status code is HTTP 200 (OK). /// </summary> /// <param name="requestActions">The <see cref="IRequestActions"/> to set up response on.</param> /// <param name="body">The <see cref="IResource"/> containing the body of the response.</param> /// <param name="headers">The response headers.</param> public static void AndRespondWith(this IRequestActions requestActions, IResource body, HttpHeaders headers) { requestActions.AndRespond(ResponseCreators.CreateWith(body, headers, HttpStatusCode.OK, "OK")); }
/// <summary> /// Creates a new instance of <see cref="HttpEntity"/> with the given headers. /// </summary> /// <param name="headers">The entity headers.</param> public HttpEntity(HttpHeaders headers) : this(null, headers) { }
private void ExpectHasMessageBody(HttpHeaders responseHeaders) { responseHeaders.ContentLength = 1; Expect.Call<HttpStatusCode>(response.StatusCode).Return(HttpStatusCode.OK).Repeat.Twice(); }
public MockHttpInputMessage(byte[] body) { this.headers = new HttpHeaders(); this.body = new MemoryStream(body); }
// private helpers private AccessGrant GetAccessGrant(MediaType responseContentType, string responseFile) { HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.ContentType = responseContentType; MockRestServiceServer mockServer = MockRestServiceServer.CreateServer(oAuth2Template.RestTemplate); mockServer.ExpectNewRequest() .AndExpectUri(ACCESS_TOKEN_URL) .AndExpectMethod(HttpMethod.POST) .AndExpectBody("client_id=client_id&client_secret=client_secret&code=code&redirect_uri=http%3A%2F%2Fwww.someclient.com%2Fcallback&grant_type=authorization_code") .AndRespondWith(new AssemblyResource(responseFile, typeof(OAuth2TemplateTests)), responseHeaders); #if NET_4_0 || SILVERLIGHT_5 AccessGrant accessGrant = oAuth2Template.ExchangeForAccessAsync("code", "http://www.someclient.com/callback", null).Result; #else AccessGrant accessGrant = oAuth2Template.ExchangeForAccess("code", "http://www.someclient.com/callback", null); #endif return accessGrant; }
/// <summary> /// Creates a <see cref="ResponseCreator"/> that respond with /// the given response body and headers. /// The response status code is HTTP 200 (OK). /// </summary> /// <param name="body">The body of the response.</param> /// <param name="headers">The response headers.</param> /// <returns>A <see cref="ResponseCreator"/>.</returns> public static ResponseCreator CreateWith(string body, HttpHeaders headers) { return CreateWith(body, headers, HttpStatusCode.OK, "OK"); }
private AccessGrant RefreshToken(MediaType responseContentType, string responseFile) { HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.ContentType = responseContentType; MockRestServiceServer mockServer = MockRestServiceServer.CreateServer(oAuth2Template.RestTemplate); mockServer.ExpectNewRequest() .AndExpectUri(ACCESS_TOKEN_URL) .AndExpectMethod(HttpMethod.POST) .AndExpectBody("client_id=client_id&client_secret=client_secret&refresh_token=r3fr35h_t0k3n&grant_type=refresh_token") .AndRespondWith(new AssemblyResource(responseFile, typeof(OAuth2TemplateTests)), responseHeaders); #if NET_4_0 || SILVERLIGHT_5 AccessGrant accessGrant = oAuth2Template.RefreshAccessAsync("r3fr35h_t0k3n", null, null).Result; #else AccessGrant accessGrant = oAuth2Template.RefreshAccess("r3fr35h_t0k3n", null, null); #endif return accessGrant; }
private HttpEntity CreateExchangeForTokenRequest(Uri tokenUrl, IDictionary<string, string> tokenParameters, NameValueCollection additionalParameters, string tokenSecret) { HttpHeaders headers = new HttpHeaders(); headers.Add("Authorization", this.signingSupport.BuildAuthorizationHeaderValue( tokenUrl, tokenParameters, additionalParameters, this.consumerKey, this.consumerSecret, tokenSecret)); if (additionalParameters == null) { additionalParameters = new NameValueCollection(); } return new HttpEntity(additionalParameters, headers); }
public void SetUp() { headers = new HttpHeaders(); }
public void ExchangeWithHeaders() { HttpHeaders headers = new HttpHeaders(); headers.ContentLength = 11; HttpEntity entity = new HttpEntity("Maryse Baia", headers); HttpResponseMessage<string> result = template.Exchange<string>( "user", HttpMethod.POST, entity); Assert.AreEqual("3", result.Body, "Invalid content"); Assert.AreEqual(HttpStatusCode.Created, result.StatusCode, "Invalid status code"); Assert.AreEqual("User id '3' created with 'Maryse Baia'", result.StatusDescription, "Invalid status description"); }
// test helpers private void AssertUserProfileImage(ImageSize imageSize) { HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.ContentType = MediaType.IMAGE_PNG; mockServer.ExpectNewRequest() .AndExpectUri("https://api.twitter.com/1/users/profile_image/brbaia?size=" + imageSize.ToString().ToLowerInvariant()) .AndExpectMethod(HttpMethod.GET) .AndRespondWith(new AssemblyResource("assembly://Spring.Social.Twitter.Tests/Spring.Social.Twitter.Api.Impl/Logo.png"), responseHeaders); #if NET_4_0 || SILVERLIGHT_5 twitter.UserOperations.GetUserProfileImageAsync("brbaia", imageSize).Wait(); #else twitter.UserOperations.GetUserProfileImage("brbaia", imageSize); #endif // TODO: Fix ResponseCreators to handle binary data so that we can assert the contents/size of the image bytes. }
public MockHttpOutputMessage() { this.headers = new HttpHeaders(); }