public WebRequest CreateWebRequest(Uri url, string method)
        {
            var request = new MockWebRequest(url);
            request.Method = method;

            return request;
        }
Пример #2
0
        public WebRequest CreateWebRequest(Uri url, string method)
        {
            var request = new MockWebRequest(url);

            request.Method = method;

            return(request);
        }
Пример #3
0
        public static string ProcessRequest(MockWebRequest request)
        {
            string responseData = null;

            if (request.RequestUri.AbsolutePath.StartsWith(FakeService.OAuthTokenEndpoint, StringComparison.OrdinalIgnoreCase))
            {
                Assert.IsTrue(request.ContentType.Equals(ContentTypeFormEncoded, StringComparison.OrdinalIgnoreCase));

                string postBody = request.RequestStream.Content;
                Assert.IsTrue(!string.IsNullOrEmpty(postBody));

                IDictionary <string, string> parameters = Helpers.ParseQueryString(postBody);
                string oldRefreshToken = parameters[AuthConstants.RefreshToken];
                Assert.IsTrue(oldRefreshToken.Equals(TestAuthClient.FakeOldRefreshToken, StringComparison.OrdinalIgnoreCase));

                return(string.Format(
                           CultureInfo.InvariantCulture,
                           ResponseJson.RefreshTokenResponse,
                           TestAuthClient.FakeAccessToken,
                           TestAuthClient.FakeRefreshToken,
                           HttpUtility.UrlDecode(LiveAuthClient.BuildScopeString(TestAuthClient.Scopes))));
            }
            else if (request.RequestUri.AbsolutePath.StartsWith(FakeService.ApiEndpoint, StringComparison.OrdinalIgnoreCase))
            {
                string authHeader = request.Headers["Authorization"];
                Assert.IsTrue(!string.IsNullOrEmpty(authHeader));
                Assert.IsTrue(authHeader.StartsWith("bearer "));

                ResourceType resourceType = GetResourceType(request.RequestUri);

                if (resourceType == ResourceType.Me)
                {
                    if (request.Method != "GET")
                    {
                        responseData = ResponseJson.ErrorInvalidMethod;
                    }
                    else
                    {
                        Assert.IsTrue(request.RequestStream == null);

                        responseData = ResponseJson.Me;
                    }
                }
                else if (resourceType == ResourceType.Contact)
                {
                    if (request.Method == "GET")
                    {
                        Assert.IsTrue(request.RequestStream == null);
                        responseData = ResponseJson.Contact;
                    }
                    else if (request.Method == "PUT")
                    {
                        Assert.IsNotNull(request.RequestStream);
                        Assert.IsNotNull(request.RequestStream.Content);
                        Assert.IsTrue(request.ContentType.Equals(ContentTypeJson));

                        Assert.IsTrue(request.RequestStream.Content.Equals(ResponseJson.Contact, StringComparison.OrdinalIgnoreCase), "Request body not written correctly.");

                        responseData = ResponseJson.Contact;
                    }
                    else if (request.Method == "DELETE")
                    {
                        Assert.IsTrue(request.RequestStream == null);
                        responseData = string.Empty;
                    }
                    else
                    {
                        responseData = ResponseJson.ErrorInvalidMethod;
                    }
                }
                else if (resourceType == ResourceType.Contacts)
                {
                    if (request.Method == "GET")
                    {
                        Assert.IsTrue(request.RequestStream == null);
                        responseData = ResponseJson.Contacts;
                    }
                    else if (request.Method == "POST")
                    {
                        Assert.IsTrue(request.ContentType.Equals(ContentTypeJson));
                        Assert.IsNotNull(request.RequestStream);
                        Assert.IsNotNull(request.RequestStream.Content);
                        Assert.IsTrue(request.RequestStream.Content.Equals(ResponseJson.Contact, StringComparison.OrdinalIgnoreCase), "Request body not written correctly.");

                        responseData = ResponseJson.Contact;
                    }
                    else
                    {
                        responseData = ResponseJson.ErrorInvalidMethod;
                    }
                }
                else if (resourceType == ResourceType.File)
                {
                    if (request.Method == "GET")
                    {
                        Assert.IsTrue(request.RequestStream == null);
                        responseData = ResponseJson.File;
                    }
                    else if (request.Method == "PUT")
                    {
                        Assert.IsTrue(request.ContentType.Equals(ContentTypeJson));
                        Assert.IsNotNull(request.RequestStream);
                        Assert.IsNotNull(request.RequestStream.Content);
                        Assert.IsTrue(request.RequestStream.Content.Equals(ResponseJson.File, StringComparison.OrdinalIgnoreCase), "Request body not written correctly.");

                        responseData = ResponseJson.File;
                    }
                    else if (request.Method == "DELETE")
                    {
                        Assert.IsTrue(request.RequestStream == null);
                        responseData = string.Empty;
                    }
                    else if (request.Method == "MOVE" || request.Method == "COPY")
                    {
                        Assert.IsTrue(request.ContentType.Equals(ContentTypeJson));
                        Assert.IsNotNull(request.RequestStream);
                        Assert.IsNotNull(request.RequestStream.Content);

                        responseData = ResponseJson.File;
                    }
                    else
                    {
                        responseData = ResponseJson.ErrorInvalidMethod;
                    }
                }
                else if (resourceType == ResourceType.Files)
                {
                    if (request.Method == "GET")
                    {
                        Assert.IsTrue(request.RequestStream == null);
                        responseData = ResponseJson.Files;
                    }
                    else if (request.Method == "POST")
                    {
                        Assert.IsTrue(request.ContentType.Equals(ContentTypeJson));
                        Assert.IsNotNull(request.RequestStream);
                        Assert.IsNotNull(request.RequestStream.Content);
                        Assert.IsTrue(request.RequestStream.Content.Equals(ResponseJson.File, StringComparison.OrdinalIgnoreCase), "Request body not written correctly.");

                        responseData = ResponseJson.Files;
                    }
                    else
                    {
                        responseData = ResponseJson.ErrorInvalidMethod;
                    }
                }
            }

            return(responseData);
        }
Пример #4
0
        public static string ProcessRequest(MockWebRequest request)
        {
            string responseData = null;

            if (request.RequestUri.AbsolutePath.StartsWith(FakeService.OAuthTokenEndpoint, StringComparison.OrdinalIgnoreCase))
            {
                Assert.IsTrue(request.ContentType.Equals(ContentTypeFormEncoded, StringComparison.OrdinalIgnoreCase));

                string postBody = request.RequestStream.Content;
                Assert.IsTrue(!string.IsNullOrEmpty(postBody));

                IDictionary<string, string> parameters = Helpers.ParseQueryString(postBody);
                string oldRefreshToken = parameters[AuthConstants.RefreshToken];
                Assert.IsTrue(oldRefreshToken.Equals(TestAuthClient.FakeOldRefreshToken, StringComparison.OrdinalIgnoreCase));

                return string.Format(
                        CultureInfo.InvariantCulture,
                        ResponseJson.RefreshTokenResponse,
                        TestAuthClient.FakeAccessToken,
                        TestAuthClient.FakeRefreshToken,
                        HttpUtility.UrlDecode(LiveAuthClient.BuildScopeString(TestAuthClient.Scopes)));
            }
            else if (request.RequestUri.AbsolutePath.StartsWith(FakeService.ApiEndpoint, StringComparison.OrdinalIgnoreCase))
            {
                string authHeader = request.Headers["Authorization"];
                Assert.IsTrue(!string.IsNullOrEmpty(authHeader));
                Assert.IsTrue(authHeader.StartsWith("bearer "));

                ResourceType resourceType = GetResourceType(request.RequestUri);

                if (resourceType == ResourceType.Me)
                {
                    if (request.Method != "GET")
                    {
                        responseData = ResponseJson.ErrorInvalidMethod;
                    }
                    else
                    {
                        Assert.IsTrue(request.RequestStream == null);

                        responseData = ResponseJson.Me;
                    }
                }
                else if (resourceType == ResourceType.Contact)
                {
                    if (request.Method == "GET")
                    {
                        Assert.IsTrue(request.RequestStream == null);
                        responseData = ResponseJson.Contact;
                    }
                    else if (request.Method == "PUT")
                    {
                        Assert.IsNotNull(request.RequestStream);
                        Assert.IsNotNull(request.RequestStream.Content);
                        Assert.IsTrue(request.ContentType.Equals(ContentTypeJson));

                        Assert.IsTrue(request.RequestStream.Content.Equals(ResponseJson.Contact, StringComparison.OrdinalIgnoreCase), "Request body not written correctly.");

                        responseData = ResponseJson.Contact;
                    }
                    else if (request.Method == "DELETE")
                    {
                        Assert.IsTrue(request.RequestStream == null);
                        responseData = string.Empty;
                    }
                    else
                    {
                        responseData = ResponseJson.ErrorInvalidMethod;
                    }
                }
                else if (resourceType == ResourceType.Contacts)
                {
                    if (request.Method == "GET")
                    {
                        Assert.IsTrue(request.RequestStream == null);
                        responseData = ResponseJson.Contacts;
                    }
                    else if (request.Method == "POST")
                    {
                        Assert.IsTrue(request.ContentType.Equals(ContentTypeJson));
                        Assert.IsNotNull(request.RequestStream);
                        Assert.IsNotNull(request.RequestStream.Content);
                        Assert.IsTrue(request.RequestStream.Content.Equals(ResponseJson.Contact, StringComparison.OrdinalIgnoreCase), "Request body not written correctly.");

                        responseData = ResponseJson.Contact;
                    }
                    else
                    {
                        responseData = ResponseJson.ErrorInvalidMethod;
                    }
                }
                else if (resourceType == ResourceType.File)
                {
                    if (request.Method == "GET")
                    {
                        Assert.IsTrue(request.RequestStream == null);
                        responseData = ResponseJson.File;
                    }
                    else if (request.Method == "PUT")
                    {
                        Assert.IsTrue(request.ContentType.Equals(ContentTypeJson));
                        Assert.IsNotNull(request.RequestStream);
                        Assert.IsNotNull(request.RequestStream.Content);
                        Assert.IsTrue(request.RequestStream.Content.Equals(ResponseJson.File, StringComparison.OrdinalIgnoreCase), "Request body not written correctly.");

                        responseData = ResponseJson.File;
                    }
                    else if (request.Method == "DELETE")
                    {
                        Assert.IsTrue(request.RequestStream == null);
                        responseData = string.Empty;
                    }
                    else if (request.Method == "MOVE" || request.Method == "COPY")
                    {
                        Assert.IsTrue(request.ContentType.Equals(ContentTypeJson));
                        Assert.IsNotNull(request.RequestStream);
                        Assert.IsNotNull(request.RequestStream.Content);

                        responseData = ResponseJson.File;
                    }
                    else
                    {
                        responseData = ResponseJson.ErrorInvalidMethod;
                    }
                }
                else if (resourceType == ResourceType.Files)
                {
                    if (request.Method == "GET")
                    {
                        Assert.IsTrue(request.RequestStream == null);
                        responseData = ResponseJson.Files;
                    }
                    else if (request.Method == "POST")
                    {
                        Assert.IsTrue(request.ContentType.Equals(ContentTypeJson));
                        Assert.IsNotNull(request.RequestStream);
                        Assert.IsNotNull(request.RequestStream.Content);
                        Assert.IsTrue(request.RequestStream.Content.Equals(ResponseJson.File, StringComparison.OrdinalIgnoreCase), "Request body not written correctly.");

                        responseData = ResponseJson.Files;
                    }
                    else
                    {
                        responseData = ResponseJson.ErrorInvalidMethod;
                    }
                }
            }

            return responseData;
        }