private AccessGrant AuthenticateClient(bool expectParamBasedClientAuthentication, string responseFile) { OAuth2Template testedOAuth2Template = expectParamBasedClientAuthentication ? oAuth2TemplateParamBasedClientAuthentication : oAuth2Template; HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.ContentType = MediaType.APPLICATION_JSON; MockRestServiceServer mockServer = MockRestServiceServer.CreateServer(testedOAuth2Template.RestTemplate); IRequestActions requestActions = mockServer.ExpectNewRequest() .AndExpectUri(ACCESS_TOKEN_URL) .AndExpectMethod(HttpMethod.POST) .AndExpectBody((expectParamBasedClientAuthentication ? "client_id=client_id&client_secret=client_secret&" : "") + "grant_type=client_credentials&scope=read%2Cwrite"); if (!expectParamBasedClientAuthentication) { requestActions.AndExpectHeader("Authorization", "Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ="); } requestActions.AndRespondWith(new AssemblyResource(responseFile, typeof(OAuth2TemplateTests)), responseHeaders); OAuth2Parameters parameters = new OAuth2Parameters(); parameters.Scope = "read,write"; #if NET_4_0 || SILVERLIGHT_5 AccessGrant accessGrant = testedOAuth2Template.AuthenticateClientAsync("read,write").Result; #else AccessGrant accessGrant = testedOAuth2Template.AuthenticateClient("read,write"); #endif return(accessGrant); }
// private helpers private AccessGrant ExchangeForAccess(bool expectParamBasedClientAuthentication, string responseFile) { OAuth2Template testedOAuth2Template = expectParamBasedClientAuthentication ? oAuth2TemplateParamBasedClientAuthentication : oAuth2Template; HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.ContentType = MediaType.APPLICATION_JSON; MockRestServiceServer mockServer = MockRestServiceServer.CreateServer(testedOAuth2Template.RestTemplate); IRequestActions requestActions = mockServer.ExpectNewRequest() .AndExpectUri(ACCESS_TOKEN_URL) .AndExpectMethod(HttpMethod.POST) .AndExpectBody((expectParamBasedClientAuthentication ? "client_id=client_id&client_secret=client_secret&" : "") + "code=code&redirect_uri=http%3A%2F%2Fwww.someclient.com%2Fcallback&grant_type=authorization_code"); if (!expectParamBasedClientAuthentication) { requestActions.AndExpectHeader("Authorization", "Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ="); } requestActions.AndRespondWith(new AssemblyResource(responseFile, typeof(OAuth2TemplateTests)), responseHeaders); #if NET_4_0 || SILVERLIGHT_5 AccessGrant accessGrant = testedOAuth2Template.ExchangeForAccessAsync("code", "http://www.someclient.com/callback", null).Result; #else AccessGrant accessGrant = testedOAuth2Template.ExchangeForAccess("code", "http://www.someclient.com/callback", null); #endif return(accessGrant); }
/// <summary> /// Expects that the specified request header contains the given substring. /// </summary> /// <param name="requestActions">The <see cref="IRequestActions"/> to set up expectation on.</param> /// <param name="header">The header name.</param> /// <param name="value">The substring that must appear in the header.</param> /// <returns> /// The <see cref="IRequestActions"/> to set up responses and additional expectations on the request. /// </returns> public static IRequestActions AndExpectHeaderContains(this IRequestActions requestActions, string header, string value) { return(requestActions.AndExpect(RequestMatchers.MatchHeaderContains(header, value))); }
/// <summary> /// Expects the given request URI. /// </summary> /// <param name="requestActions">The <see cref="IRequestActions"/> to set up expectation on.</param> /// <param name="uri">the request URI.</param> /// <returns> /// The <see cref="IRequestActions"/> to set up responses and additional expectations on the request. /// </returns> public static IRequestActions AndExpectUri(this IRequestActions requestActions, Uri uri) { return(requestActions.AndExpect(RequestMatchers.MatchUri(uri))); }
/// <summary> /// Expects the given request <see cref="HttpMethod"/>. /// </summary> /// <param name="requestActions">The <see cref="IRequestActions"/> to set up expectation on.</param> /// <param name="method">The HTTP method.</param> /// <returns> /// The <see cref="IRequestActions"/> to set up responses and additional expectations on the request. /// </returns> public static IRequestActions AndExpectMethod(this IRequestActions requestActions, HttpMethod method) { return(requestActions.AndExpect(RequestMatchers.MatchMethod(method))); }
/// <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> /// 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> /// Expects that the request body contains the given substring. /// </summary> /// <param name="requestActions">The <see cref="IRequestActions"/> to set up expectation on.</param> /// <param name="body">The request body.</param> /// <returns> /// The <see cref="IRequestActions"/> to set up responses and additional expectations on the request. /// </returns> public static IRequestActions AndExpectBodyContains(this IRequestActions requestActions, string body) { return(requestActions.AndExpect(RequestMatchers.MatchBodyContains(body))); }
public RequestManager(long timestamp, IRequestActions requestActions) { Timestamp = timestamp; Action = requestActions; }