public void RetryAuthenticatedRequestWithCorrectedTimestamp_ResultCodeIs200() { m_appContext = CreateAppContextWithDelay(m_appId, m_appKey, TEST_TIME_DELAY); var userContext = CreateUserOperationContext(); var uri = userContext.CreateAuthenticatedUri(GET_ORGANIZATION_INFO_ROUTE, "GET"); var request = (HttpWebRequest)WebRequest.Create(uri); request.Method = "GET"; try { request.GetResponse(); } catch (WebException ex) { var exWrapper = new D2LWebException(ex); userContext.InterpretResult(exWrapper); } var retryUri = userContext.CreateAuthenticatedUri(GET_ORGANIZATION_INFO_ROUTE, "GET"); m_asyncRequest = (HttpWebRequest)WebRequest.Create(retryUri); m_asyncRequest.Method = "GET"; m_getResponseAsyncCallbackHandle = new EventWaitHandle(false, EventResetMode.AutoReset); m_asyncRequest.BeginGetResponse(GetResponseAsyncCallback, null); m_getResponseAsyncCallbackHandle.WaitOne(60 * 1000); Assert.AreEqual(HttpStatusCode.OK, m_asyncResponseStatusCode); }
public void SendRequestWithBadKeys_ResponseInterpretationIs_InvalidSig() { var request = PrepareApiRequest(m_badUserContext, GET_ORGANIZATION_INFO_ROUTE); try { request.GetResponse(); } catch (WebException ex) { var exceptionWrapper = new D2LWebException(ex); var interpretation = m_userContext.InterpretResult(exceptionWrapper); Assert.AreEqual(RequestResult.RESULT_INVALID_SIG, interpretation); } }
public void SendRequestWithBadKeys_ResponseInterpretationIs_InvalidSig() { ID2LUserContext badContext = ContextProvider.BadUserContext(); HttpWebRequest request = RequestProvider.PrepareApiRequest(badContext, RouteProvider.OrganizationInfoRoute); try { using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { } } catch (WebException ex) { var exceptionWrapper = new D2LWebException(ex); var interpretation = badContext.InterpretResult(exceptionWrapper); Assert.AreEqual(RequestResult.RESULT_INVALID_SIG, interpretation); } }
private HttpWebResponse RetryGetRequest(ID2LUserContext opContext, string route) { var mainRequest = CreateGetRequestForRoute(opContext, route); try { return(mainRequest.GetResponse() as HttpWebResponse); } catch (WebException ex) { var exWrapper = new D2LWebException(ex); opContext.InterpretResult(exWrapper); } var retryRequest = CreateGetRequestForRoute(opContext, route); return(retryRequest.GetResponse() as HttpWebResponse); }
RequestResult ID2LUserContext.InterpretResult( D2LWebException exceptionWrapper ) { switch( exceptionWrapper.StatusCode ) { case HttpStatusCode.Forbidden: return InterpretStatusCodeForbidden( exceptionWrapper.ResponseBody ); case HttpStatusCode.BadRequest: return RequestResult.BAD_REQUEST; case HttpStatusCode.NotFound: return RequestResult.NOT_FOUND; case HttpStatusCode.InternalServerError: return RequestResult.INTERNAL_SERVER_ERROR; default: return RequestResult.RESULT_UNKNOWN; } }
RequestResult ID2LUserContext.InterpretResult(D2LWebException exceptionWrapper) { switch (exceptionWrapper.StatusCode) { case HttpStatusCode.Forbidden: return(InterpretStatusCodeForbidden(exceptionWrapper.ResponseBody)); case HttpStatusCode.BadRequest: return(RequestResult.BAD_REQUEST); case HttpStatusCode.NotFound: return(RequestResult.NOT_FOUND); case HttpStatusCode.InternalServerError: return(RequestResult.INTERNAL_SERVER_ERROR); default: return(RequestResult.RESULT_UNKNOWN); } }
public static void Perform(HttpWebRequest request, ID2LUserContext userContext, HandleResponse responseHandler, HandleError errorHandler, int retryAttempts = 0) { try { using (var response = (HttpWebResponse)request.GetResponse()) { using (var stream = response.GetResponseStream()) { using (var reader = new StreamReader(stream, Encoding.UTF8)) { string responseBody = reader.ReadToEnd(); responseHandler(responseBody); } } } } catch (WebException we) { var exceptionWrapper = new D2LWebException(we); var result = userContext.InterpretResult(exceptionWrapper); switch (result) { case RequestResult.RESULT_INVALID_TIMESTAMP: if (retryAttempts > 0) { // re-create the request to ensure the new url accounts for calculated skew Uri uri = userContext.CreateAuthenticatedUri(request.RequestUri.AbsoluteUri, request.Method); request = (HttpWebRequest)WebRequest.Create(uri); Perform(request, userContext, responseHandler, errorHandler, retryAttempts - 1); } break; } errorHandler(result, request.RequestUri); } catch (ArgumentException) { errorHandler(RequestResult.RESULT_UNKNOWN, request.RequestUri); } }
public void RetryAuthenticatedRequestWithCorrectedTimestamp_ResultCodeIs200() { ID2LAppContext appContext = CreateAppContextWithDelay( ConfigHelper.AppId, ConfigHelper.AppKey, TEST_TIME_DELAY ); ID2LUserContext userContext = ContextProvider.UserContext( appContext ); Uri uri = userContext.CreateAuthenticatedUri( RouteProvider.OrganizationInfoRoute, "GET" ); HttpWebRequest request = (HttpWebRequest)WebRequest.Create( uri ); request.Method = "GET"; try { using( request.GetResponse() as HttpWebResponse ) { } } catch( WebException ex ) { var exWrapper = new D2LWebException( ex ); userContext.InterpretResult( exWrapper ); } Uri retryUri = userContext.CreateAuthenticatedUri( RouteProvider.OrganizationInfoRoute, "GET" ); HttpWebRequest retryRequest = (HttpWebRequest)WebRequest.Create( retryUri ); retryRequest.Method = "GET"; Assert.DoesNotThrow( () => { using( HttpWebResponse response = retryRequest.GetResponse() as HttpWebResponse ) { } } ); }
public void RetryAuthenticatedRequestWithCorrectedTimestamp_ResultCodeIs200() { ID2LAppContext appContext = CreateAppContextWithDelay(ConfigHelper.AppId, ConfigHelper.AppKey, TEST_TIME_DELAY); ID2LUserContext userContext = ContextProvider.UserContext(appContext); Uri uri = userContext.CreateAuthenticatedUri(RouteProvider.OrganizationInfoRoute, "GET"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.Method = "GET"; try { using (request.GetResponse() as HttpWebResponse) { } } catch (WebException ex) { var exWrapper = new D2LWebException(ex); userContext.InterpretResult(exWrapper); } Uri retryUri = userContext.CreateAuthenticatedUri(RouteProvider.OrganizationInfoRoute, "GET"); HttpWebRequest retryRequest = (HttpWebRequest)WebRequest.Create(retryUri); retryRequest.Method = "GET"; Assert.DoesNotThrow(() => { using (HttpWebResponse response = retryRequest.GetResponse() as HttpWebResponse) { } }); }
public void SendRequestWithBadKeys_ResponseInterpretationIs_InvalidSig() { ID2LUserContext badContext = ContextProvider.BadUserContext(); HttpWebRequest request = RequestProvider.PrepareApiRequest( badContext, RouteProvider.OrganizationInfoRoute ); try { using( HttpWebResponse response = request.GetResponse() as HttpWebResponse ) { } } catch( WebException ex ) { var exceptionWrapper = new D2LWebException( ex ); var interpretation = badContext.InterpretResult( exceptionWrapper ); Assert.AreEqual( RequestResult.RESULT_INVALID_SIG, interpretation ); } }