Beispiel #1
0
        public async Task IsItPossibleToCreateNewUser()
        {
            var user = new Eager.User()
            {
                Username = "******",
                Password = "******",
                Email    = "*****@*****.**"
            };
            var content = new StringContent(JsonConvert.SerializeObject(user), Encoding.UTF8, "application/json");

            var userBadData = new Eager.User()
            {
                Username = "",
                Password = "******",
                Email    = "post@user"
            };
            var contentBadData = new StringContent(JsonConvert.SerializeObject(userBadData), Encoding.UTF8, "application/json");

            var unauthResponse = await DefaultClient.PostAsync("api/Users", content);

            Assert.Equal(HttpStatusCode.Unauthorized, unauthResponse.StatusCode);

            var badResponse = await AuthorizedClient.PostAsync("api/Users", contentBadData);

            Assert.Equal(HttpStatusCode.BadRequest, badResponse.StatusCode);

            var response = await AuthorizedClient.PostAsync("api/Users", content);

            var responseContent = await response.Content.ReadAsStringAsync();

            Assert.Equal(HttpStatusCode.Created, response.StatusCode);
        }
Beispiel #2
0
        public async Task CheckIfCreatingCategoryIsCorret(string categoryName, HttpStatusCode responseStatus)
        {
            var category = new Models.Eager.Category()
            {
                Name = categoryName
            };
            var jsonString = JsonConvert.SerializeObject(category);
            var content    = new StringContent(jsonString, Encoding.UTF8, "application/json");

            var response = await AuthorizedClient.PostAsync("api/Categories", content);

            Assert.Equal(responseStatus, response.StatusCode);
        }
Beispiel #3
0
        /// <summary>
        /// utility method to perform HTTP POST for Twitter requests with side-effects
        /// </summary>
        /// <param name="url">URL of request</param>
        /// <param name="parameters">parameters to post</param>
        /// <param name="reqProc">Processes results of async requests</param>
        /// <returns>XML response from Twitter</returns>
        public string ExecuteTwitter <T>(string url, Dictionary <string, string> parameters, IRequestProcessor <T> reqProc)
        {
            string httpStatus  = string.Empty;
            string responseXml = string.Empty;

            // Oddly, we add the parameters both to the URI's query string and the POST entity
            Uri requestUri = Utilities.AppendQueryString(new Uri(url), parameters);

            try
            {
                this.LastUrl = requestUri.AbsoluteUri;
                //Log
                WriteLog(url, "ExecuteTwitter");

#if SILVERLIGHT
                HttpWebRequest req = AuthorizedClient.PostAsync(url, parameters);

                req.BeginGetResponse(
                    new AsyncCallback(
                        ar =>
                {
                    var resp    = req.EndGetResponse(ar) as HttpWebResponse;
                    responseXml = GetTwitterResponse(resp);
                    CheckResultsForTwitterError(responseXml, httpStatus);

                    if (AsyncCallback != null)
                    {
                        List <T> responseObj = reqProc.ProcessResults(responseXml);
                        var asyncResp        = new TwitterAsyncResponse <T>();
                        asyncResp.State      = responseObj.FirstOrDefault();
                        (AsyncCallback as Action <TwitterAsyncResponse <T> >)(asyncResp);
                    }
                }), null);
#else
                if (AsyncCallback != null)
                {
                    HttpWebRequest req = AuthorizedClient.PostAsync(url, parameters);

                    req.BeginGetResponse(
                        new AsyncCallback(
                            ar =>
                    {
                        var resp    = req.EndGetResponse(ar) as HttpWebResponse;
                        responseXml = GetTwitterResponse(resp);
                        CheckResultsForTwitterError(responseXml, httpStatus);

                        List <T> responseObj = reqProc.ProcessResults(responseXml);
                        var asyncResp        = new TwitterAsyncResponse <T>();
                        asyncResp.State      = responseObj.FirstOrDefault();
                        (AsyncCallback as Action <TwitterAsyncResponse <T> >)(asyncResp);
                    }),
                        null);
                }
                else
                {
                    using (var resp = this.AuthorizedClient.Post(url, parameters))
                    {
                        httpStatus  = resp.Headers["Status"];
                        responseXml = GetTwitterResponse(resp);
                    }
                }
#endif
            }
            catch (WebException wex)
            {
                var twitterQueryEx = CreateTwitterQueryException(wex);
                throw twitterQueryEx;
            }

            return(responseXml);
        }