public void CreateOrder()
        {
            responseHeaders.Location = new Uri("http://restbucks.com/order/123456");
            mockServer.ExpectNewRequest()
            .AndExpectUri("http://restbuckson.net/orders")
            .AndExpectMethod(HttpMethod.POST)
            .AndExpectBody("<order xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://restbucks.com\"><location>inShop</location><items><item><name>Latte</name><quantity>1</quantity></item></items></order>")
            .AndRespondWith("", responseHeaders);

            Order order = new Order()
            {
                Location = "inShop",
                Items    = new List <OrderItem>()
                {
                    new OrderItem()
                    {
                        Name     = "Latte",
                        Quantity = 1
                    }
                }
            };
            Uri uri = restBucksClient.CreateOrder(order);

            Assert.AreEqual(responseHeaders.Location, uri);
        }
예제 #2
0
        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);
        }
예제 #3
0
        // 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);
        }
        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);
        }
        public void ExchangeForAccessToken_OAuth10()
        {
            MockRestServiceServer mockServer      = MockRestServiceServer.CreateServer(oauth10.RestTemplate);
            HttpHeaders           responseHeaders = new HttpHeaders();

            responseHeaders.ContentType = MediaType.APPLICATION_FORM_URLENCODED;
            mockServer.ExpectNewRequest()
            .AndExpectUri(ACCESS_TOKEN_URL)
            .AndExpectMethod(HttpMethod.POST)
            .AndExpectHeaderContains("Authorization", "oauth_version=\"1.0\"")
            .AndExpectHeaderContains("Authorization", "oauth_signature_method=\"HMAC-SHA1\"")
            .AndExpectHeaderContains("Authorization", "oauth_consumer_key=\"consumer_key\"")
            .AndExpectHeaderContains("Authorization", "oauth_token=\"1234567890\"")
            .AndExpectHeaderContains("Authorization", "oauth_nonce=\"")
            .AndExpectHeaderContains("Authorization", "oauth_signature=\"")
            .AndExpectHeaderContains("Authorization", "oauth_timestamp=\"")
            .AndRespondWith(EmbeddedResource("AccessToken.formencoded"), responseHeaders);

            OAuthToken requestToken = new OAuthToken("1234567890", "abcdefghijklmnop");

#if NET_4_0 || SILVERLIGHT_5
            OAuthToken accessToken = oauth10.ExchangeForAccessTokenAsync(new AuthorizedRequestToken(requestToken, "verifier"), null).Result;
#else
            OAuthToken accessToken = oauth10.ExchangeForAccessToken(new AuthorizedRequestToken(requestToken, "verifier"), null);
#endif
            Assert.AreEqual("9876543210", accessToken.Value);
            Assert.AreEqual("ponmlkjihgfedcba", accessToken.Secret);
        }
        public void PathNotFound()
        {
            mockServer.ExpectNewRequest()
            .AndExpectUri("https://api.dropbox.com/1/metadata/sandbox/Spring Social/Temp.txt")
            .AndExpectMethod(HttpMethod.GET)
            .AndRespondWith("{\"error\": \"Path '/Spring Social/Temp.txt' not found\"}", responseHeaders, HttpStatusCode.NotFound, "");

#if NET_4_0 || SILVERLIGHT_5
            dropbox.GetMetadataAsync("/Spring Social/Temp.txt")
            .ContinueWith(task =>
            {
                AssertDropboxApiException(task.Exception, "Path '/Spring Social/Temp.txt' not found", DropboxApiError.PathNotFound);
            })
            .Wait();
#else
            try
            {
                dropbox.GetMetadata("/Spring Social/Temp.txt");
                Assert.Fail("Exception expected");
            }
            catch (Exception ex)
            {
                AssertDropboxApiException(ex, "Path '/Spring Social/Temp.txt' not found", DropboxApiError.PathNotFound);
            }
#endif
        }
예제 #7
0
        // private helpers

        private static void AssertThatRequestInterceptorWritesAuthorizationHeader(AbstractOAuth2ApiBinding apiBinding, string expectedAuthorizationHeaderValue)
        {
            MockRestServiceServer mockServer = MockRestServiceServer.CreateServer(apiBinding.RestTemplate);

            mockServer.ExpectNewRequest()
            .AndExpectUri("https://api.someprovider.com/status/update")
            .AndExpectMethod(HttpMethod.POST)
            .AndExpectHeader("Content-Type", MediaType.APPLICATION_FORM_URLENCODED.ToString())
            .AndExpectHeader("Authorization", expectedAuthorizationHeaderValue)
            .AndExpectBody("status=Hello+there%21");
        }
예제 #8
0
        public void Should_respond_Created_on_new_common_data_upload_as_json()
        {
            const string expectedKey   = "aaa";
            const string expectedValue = "bbb";
            var          url           = GIVEN_url_to_testRun_data();

            _responseHeaders.Location = new Uri(BaseUrl + url);
            _mockRestServer.ExpectNewRequest()
            .AndExpectUri(BaseUrl + url)
            .AndExpectMethod(HttpMethod.POST)
            .AndExpectBodyContains(expectedKey)
            .AndExpectBodyContains(expectedValue)
            .AndRespondWith("", _responseHeaders, HttpStatusCode.Created, "");

            var commonDataSender = ProxyFactory.Get <CommonDataSender>();

            commonDataSender.Send(new CommonDataItem {
                Key = expectedKey, Value = expectedValue
            });
        }
예제 #9
0
        public void RequestInterceptor()
        {
            AbstractOAuth1ApiBindingTests apiBinding = new AbstractOAuth1ApiBindingTests("consumer_key", "consumer_secret", "access_token", "token_secret");

            MockRestServiceServer mockServer = MockRestServiceServer.CreateServer(apiBinding.RestTemplate);

            mockServer.ExpectNewRequest()
            .AndExpectUri("https://api.someprovider.com/status/update")
            .AndExpectMethod(HttpMethod.POST)
            .AndExpectHeader("Content-Type", MediaType.APPLICATION_FORM_URLENCODED.ToString())
            .AndExpect(MatchHeaderStartsWith("Authorization", "OAuth "))
            .AndExpectHeaderContains("Authorization", "oauth_version=\"1.0\"")
            .AndExpectHeaderContains("Authorization", "oauth_nonce=\"")
            .AndExpectHeaderContains("Authorization", "oauth_signature_method=\"HMAC-SHA1\"")
            .AndExpectHeaderContains("Authorization", "oauth_consumer_key=\"consumer_key\"")
            .AndExpectHeaderContains("Authorization", "oauth_token=\"access_token\"")
            .AndExpectHeaderContains("Authorization", "oauth_timestamp=\"")
            .AndExpectHeaderContains("Authorization", "oauth_signature=\"")
            .AndExpectBody("status=Hello+there%21");

            apiBinding.UpdateStatus();
        }
        public void GetUserProfile()
        {
            mockServer.ExpectNewRequest()
            .AndExpectUri("https://api.dropbox.com/1/account/info")
            .AndExpectMethod(HttpMethod.GET)
            .AndRespondWith(EmbeddedResource("Dropbox_Profile.json"), responseHeaders);

#if NET_4_0 || SILVERLIGHT_5
            DropboxProfile profile = dropbox.GetUserProfileAsync().Result;
#else
            DropboxProfile profile = dropbox.GetUserProfile();
#endif
            Assert.AreEqual("US", profile.Country);
            Assert.AreEqual("John P. User", profile.DisplayName);
            Assert.AreEqual("*****@*****.**", profile.Email);
            Assert.AreEqual(12345678, profile.ID);
            Assert.AreEqual(107374182400000, profile.Quota);
            Assert.AreEqual(680031877871, profile.QuotaNormal);
            Assert.AreEqual(253738410565, profile.QuotaShared);
            Assert.AreEqual("https://www.dropbox.com/referrals/r1a2n3d4m5s6t7", profile.ReferralLink);
        }