public string GetMessages(string uri, Args args = null)
        {
            var request = new RESTRequest(uri);

            if (args != null)
            {
                foreach (var arg in args)
                {
                    request.AddQuery(arg.Key, arg.Value + "");
                }
            }

            return(_client.Execute(request).Content);
        }
        public void Create()
        {
            using (MSSQLDbClient sqlClient = new MSSQLDbClient(ConnectionString))
            {
                ProductRowApiO existing = sqlClient.Fill <ProductRowApiO>(GetRecordSql);
                if (existing != null)
                {
                    this.Delete();
                }

                using (RESTClient restClient = new RESTClient(BaseUrl))
                {
                    ProductRowApiO newItem = new ProductRowApiO()
                    {
                        ProductName = TestProductName, CategoryId = 2, QuantityPerUnit = "4 in a box", UnitPrice = (decimal)12.37, SupplierId = 1
                    };

                    RestResult <ProductRowApiO> apiResult = restClient.Execute <ProductRowApiO>("Product", RestSharp.Method.PUT, jsonBody: newItem, headerParameters: Headers);
                    Assert.True(apiResult.StatusCode == 201, apiResult.Content);

                    ProductRowApiO sqlResult = sqlClient.Fill <ProductRowApiO>(GetRecordSql);
                    Assert.True(sqlResult.ProductName == apiResult.Result.ProductName &&
                                sqlResult.CategoryId == apiResult.Result.CategoryId &&
                                sqlResult.QuantityPerUnit == apiResult.Result.QuantityPerUnit &&
                                sqlResult.ProductId == apiResult.Result.ProductId);
                }
            }
        }
Beispiel #3
0
        public void UpdatePartial()
        {
            using (MSSQLDbClient sqlClient = new MSSQLDbClient(ConnectionString))
            {
                CategoryRowApiO existingCategory = sqlClient.Fill <CategoryRowApiO>(GetRecordSql);

                if (existingCategory == null)
                {
                    this.Create();
                    existingCategory = sqlClient.Fill <CategoryRowApiO>(GetRecordSql);
                }

                Assert.NotNull(existingCategory);

                using (RESTClient restClient = new RESTClient(BaseUrl))
                {
                    string partialJson = "{ \"CategoryId\": " + existingCategory.CategoryId + ", \"Description\": \"All about more text\" }";
                    RestResult <CategoryRowApiO> apiResult = restClient.Execute <CategoryRowApiO>("Category", RestSharp.Method.PATCH, jsonBodyPartial: partialJson, headerParameters: Headers);
                    Assert.True(apiResult.StatusCode == 200, apiResult.Content);
                }

                CategoryRowApiO dbCategory = sqlClient.Fill <CategoryRowApiO>(GetRecordSql);
                Assert.True(dbCategory.Description == "All about more text", "Incorrect text was saved.");
            }
        }
Beispiel #4
0
        public void Create()
        {
            using (MSSQLDbClient sqlClient = new MSSQLDbClient(ConnectionString))
            {
                CategoryRowApiO existingCategory = sqlClient.Fill <CategoryRowApiO>(GetRecordSql);
                if (existingCategory != null)
                {
                    this.Delete();
                }

                using (RESTClient restClient = new RESTClient(BaseUrl))
                {
                    CategoryRowApiO newCategory = new CategoryRowApiO()
                    {
                        CategoryName = TestCategoryName, Description = "Some text about the item"
                    };

                    RestResult <CategoryRowApiO> apiResult = restClient.Execute <CategoryRowApiO>("Category", RestSharp.Method.PUT, jsonBody: newCategory, headerParameters: Headers);
                    Assert.True(apiResult.StatusCode == 201, apiResult.Content);

                    CategoryRowApiO sqlResult = sqlClient.Fill <CategoryRowApiO>("SELECT CategoryID as CategoryId, CategoryName, Description FROM Categories WHERE CategoryName = '" + newCategory.CategoryName + "' ORDER BY CategoryID");
                    Assert.True(sqlResult.CategoryName == apiResult.Result.CategoryName &&
                                sqlResult.Description == apiResult.Result.Description &&
                                sqlResult.CategoryId == apiResult.Result.CategoryId);
                }
            }
        }
        public void Update()
        {
            using (MSSQLDbClient sqlClient = new MSSQLDbClient(ConnectionString))
            {
                ProductRowApiO existing = sqlClient.Fill <ProductRowApiO>(GetRecordSql);

                if (existing == null)
                {
                    this.Create();
                    existing = sqlClient.Fill <ProductRowApiO>(GetRecordSql);
                }

                Assert.NotNull(existing);

                using (RESTClient restClient = new RESTClient(BaseUrl))
                {
                    ProductRowApiO updated = existing;
                    updated.UnitPrice = 123;

                    RestResult <ProductRowApiO> apiResult = restClient.Execute <ProductRowApiO>("Product", RestSharp.Method.PATCH, jsonBody: updated, headerParameters: Headers);
                    Assert.True(apiResult.StatusCode == 200, apiResult.Content);
                    Assert.True(apiResult.Result.UnitPrice == 123, "The updated value was not returned");
                }

                ProductRowApiO dbValue = sqlClient.Fill <ProductRowApiO>(GetRecordSql);
                Assert.True(dbValue.UnitPrice == 123, "The updated value was not returned");
            }
        }
        public void UpdatePartial()
        {
            using (MSSQLDbClient sqlClient = new MSSQLDbClient(ConnectionString))
            {
                ProductRowApiO existing = sqlClient.Fill <ProductRowApiO>(GetRecordSql);

                if (existing == null)
                {
                    this.Create();
                    existing = sqlClient.Fill <ProductRowApiO>(GetRecordSql);
                }

                Assert.NotNull(existing);

                using (RESTClient restClient = new RESTClient(BaseUrl))
                {
                    string partialJson = "{ \"ProductId\": " + existing.ProductId + ", \"CategoryId\": 2, \"SupplierId\": 1, \"UnitPrice\": 32.1 }";
                    RestResult <ProductRowApiO> apiResult = restClient.Execute <ProductRowApiO>("Product", RestSharp.Method.PATCH, jsonBodyPartial: partialJson, headerParameters: Headers);
                    Assert.True(apiResult.StatusCode == 200, apiResult.Content);

                    ProductRowApiO dbValue = sqlClient.Fill <ProductRowApiO>(GetRecordSql);
                    Assert.True(dbValue.UnitPrice == (decimal)32.1, "Incorrect text was saved.");
                }
            }
        }
        public void UpdatePartialBadly()
        {
            using (MSSQLDbClient sqlClient = new MSSQLDbClient(ConnectionString))
            {
                ProductRowApiO existingCategory = sqlClient.Fill <ProductRowApiO>(GetRecordSql);

                if (existingCategory == null)
                {
                    this.Create();
                    existingCategory = sqlClient.Fill <ProductRowApiO>(GetRecordSql);
                }

                Assert.NotNull(existingCategory);

                using (RESTClient restClient = new RESTClient(BaseUrl))
                {
                    string partialJson = "{ \"ProductId\": " + existingCategory.ProductId + " }";
                    RestResult <ProductRowApiO> apiResult = restClient.Execute <ProductRowApiO>("Product", RestSharp.Method.PATCH, jsonBodyPartial: partialJson, headerParameters: Headers);

                    Assert.False(apiResult.StatusCode == 200, apiResult.Content);
                }

                ProductRowApiO dbValue = sqlClient.Fill <ProductRowApiO>(GetRecordSql);
                Assert.False(dbValue.ProductName == string.Empty, "Incorrect text was saved.");
            }
        }
        public void Delete()
        {
            using (MSSQLDbClient sqlClient = new MSSQLDbClient(ConnectionString))
            {
                ProductRowApiO existingCategory = sqlClient.Fill <ProductRowApiO>(GetRecordSql);

                if (existingCategory == null)
                {
                    this.Create();
                    existingCategory = sqlClient.Fill <ProductRowApiO>(GetRecordSql);
                }

                Assert.NotNull(existingCategory);

                using (RESTClient restClient = new RESTClient(BaseUrl))
                {
                    List <KeyValuePair <string, string> > routeParams = new List <KeyValuePair <string, string> >
                    {
                        new KeyValuePair <string, string>("key", existingCategory.ProductId.ToString())
                    };

                    RestResult <ProductRowApiO> apiResult = restClient.Execute <ProductRowApiO>("Product/{key}", RestSharp.Method.DELETE, routeParameters: routeParams, headerParameters: Headers);
                    Assert.True(apiResult.StatusCode == 301, apiResult.Content);
                }

                ProductRowApiO dbValue = sqlClient.Fill <ProductRowApiO>(GetRecordSql);
                Assert.Null(dbValue);
            }
        }
Beispiel #9
0
        public void UpdatePartialBadly()
        {
            using (MSSQLDbClient sqlClient = new MSSQLDbClient(ConnectionString))
            {
                CategoryRowApiO existingCategory = sqlClient.Fill <CategoryRowApiO>(GetRecordSql);

                if (existingCategory == null)
                {
                    this.Create();
                    existingCategory = sqlClient.Fill <CategoryRowApiO>(GetRecordSql);
                }

                Assert.NotNull(existingCategory);

                using (RESTClient restClient = new RESTClient(BaseUrl))
                {
                    string partialJson = "{ \"CategoryId\": " + existingCategory.CategoryId + " }";
                    RestResult <CategoryRowApiO> apiResult = restClient.Execute <CategoryRowApiO>("Category", RestSharp.Method.PATCH, jsonBodyPartial: partialJson, headerParameters: Headers);

                    // the assert should be false. The model we are working with is simple and has only 1 required parameter.
                    Assert.True(apiResult.StatusCode == 200, apiResult.Content);
                }

                CategoryRowApiO dbCategory = sqlClient.Fill <CategoryRowApiO>(GetRecordSql);
                Assert.False(dbCategory.CategoryName == string.Empty, "Incorrect text was saved.");
            }
        }
Beispiel #10
0
        public void Update()
        {
            using (MSSQLDbClient sqlClient = new MSSQLDbClient(ConnectionString))
            {
                CategoryRowApiO existingCategory = sqlClient.Fill <CategoryRowApiO>(GetRecordSql);

                if (existingCategory == null)
                {
                    this.Create();
                    existingCategory = sqlClient.Fill <CategoryRowApiO>(GetRecordSql);
                }

                Assert.NotNull(existingCategory);

                using (RESTClient restClient = new RESTClient(BaseUrl))
                {
                    CategoryRowApiO updatedCategory = existingCategory;
                    updatedCategory.Description += " More text.";

                    RestResult <CategoryRowApiO> apiResult = restClient.Execute <CategoryRowApiO>("Category", RestSharp.Method.PATCH, jsonBody: updatedCategory, headerParameters: Headers);
                    Assert.True(apiResult.StatusCode == 200, apiResult.Content);
                    Assert.True(apiResult.Result.Description.EndsWith(" More text."), "The updated text was not returned");
                }

                CategoryRowApiO dbCategory = sqlClient.Fill <CategoryRowApiO>(GetRecordSql);
                Assert.True(dbCategory.Description.EndsWith(" More text."), "The updated text was not returned");
            }
        }
        public void UpdateInvalid()
        {
            using (RESTClient restClient = new RESTClient(BaseUrl))
            {
                string partialJson = "{ \"ProductId\": -12345, \"CategoryId\": 2, \"SupplierId\":1, \"UnitPrice\": 0 }";

                RestResult <ProductRowApiO> apiResult = restClient.Execute <ProductRowApiO>("Product", RestSharp.Method.PATCH, jsonBodyPartial: partialJson, headerParameters: Headers);
                Assert.True(apiResult.StatusCode == 406 || apiResult.StatusCode == 400 || apiResult.StatusCode == 204, apiResult.Content);
            }
        }
Beispiel #12
0
        public void UpdateInvalid()
        {
            using (RESTClient restClient = new RESTClient(BaseUrl))
            {
                string partialJson = "{ \"CategoryId\": -12345, \"Description\": \"All about more text\" }";

                RestResult <CategoryRowApiO> apiResult = restClient.Execute <CategoryRowApiO>("Category", RestSharp.Method.PATCH, jsonBodyPartial: partialJson, headerParameters: Headers);
                Assert.True(apiResult.StatusCode == 406 || apiResult.StatusCode == 400 || apiResult.StatusCode == 204, apiResult.Content);
            }
        }
Beispiel #13
0
 public void CreateInvalid()
 {
     using (RESTClient restClient = new RESTClient(BaseUrl))
     {
         CategoryRowApiO newCategory = new CategoryRowApiO()
         {
             CategoryName = string.Empty, Description = "Some text about the item"
         };
         RestResult <CategoryRowApiO> apiResult = restClient.Execute <CategoryRowApiO>("Category", RestSharp.Method.PUT, jsonBody: newCategory, headerParameters: Headers);
         Assert.True(apiResult.StatusCode == 406 || apiResult.StatusCode == 400, apiResult.Content);
     }
 }
 public void CreateInvalid()
 {
     using (RESTClient restClient = new RESTClient(BaseUrl))
     {
         ProductRowApiO newItem = new ProductRowApiO()
         {
             ProductName = string.Empty, UnitPrice = 0, SupplierId = -3, CategoryId = -4
         };
         RestResult <CategoryRowApiO> apiResult = restClient.Execute <CategoryRowApiO>("Product", RestSharp.Method.PUT, jsonBody: newItem, headerParameters: Headers);
         Assert.True(apiResult.StatusCode == 406 || apiResult.StatusCode == 400, apiResult.Content);
     }
 }
        public void DeleteInvalid()
        {
            using (RESTClient restClient = new RESTClient(BaseUrl))
            {
                List <KeyValuePair <string, string> > routeParams = new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("key", "-1234")
                };

                RestResult <CategoryRowApiO> apiResult = restClient.Execute <CategoryRowApiO>("Product/{key}", RestSharp.Method.DELETE, routeParameters: routeParams, headerParameters: Headers);
                Assert.True(apiResult.StatusCode == 500 || apiResult.StatusCode == 400 || apiResult.StatusCode == 204, apiResult.Content);
            }
        }
        public void GetNonExistant()
        {
            using (RESTClient restClient = new RESTClient(BaseUrl))
            {
                List <KeyValuePair <string, string> > routeParams = new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("key", "1000")
                };

                RestResult <ProductRowApiO> apiResult = restClient.Execute <ProductRowApiO>("Product/{key}", RestSharp.Method.POST, routeParameters: routeParams, headerParameters: Headers);
                Assert.True(apiResult.StatusCode == 204, apiResult.Content);
                Assert.Null(apiResult.Result);
            }
        }
Beispiel #17
0
        static void Main(string[] args)
        {
            RESTClient gc = new RESTClient("http://localhost:1234");
            //gc.NetworkCredentials = new Grapevine.NetworkCredential("username", "password");
            //var cookie = new Grapevine.Cookie("sessionid", "A113");
            //gc.Cookies.Add(cookie);
            var request = new RESTRequest("/user/{id}");

            request.AddParameter("id", "1234");
            request.Method      = Grapevine.HttpMethod.GET;
            request.ContentType = Grapevine.ContentType.JSON;
            request.Payload     = "{\"key\":\"value\"}";
            request.ContentType = Grapevine.ContentType.JSON;
            request.Payload     = "{\"key\":\"value\"}";
            var response = gc.Execute(request);
        }
Beispiel #18
0
        public void GetAll()
        {
            using (RESTClient restClient = new RESTClient(BaseUrl))
            {
                RestResult <List <CategoryRowApiO> > apiResult = restClient.Execute <List <CategoryRowApiO> >("Category", RestSharp.Method.GET, headerParameters: Headers);
                Assert.True(apiResult.Success, apiResult.Content);
                Assert.NotNull(apiResult.Result);

                using (MSSQLDbClient sqlClient = new MSSQLDbClient(ConnectionString))
                {
                    List <CategoryRowApiO> sqlResult = sqlClient.Fill <List <CategoryRowApiO> >("SELECT CategoryID, CategoryName, Description FROM Categories ORDER BY CategoryID");
                    Assert.NotNull(sqlResult);

                    Assert.True(apiResult.Result.Count == sqlResult.Count, "The record counts do not match.");
                }
            }
        }
        public void GetAll()
        {
            using (RESTClient restClient = new RESTClient(BaseUrl))
            {
                RestResult <List <ProductRowApiO> > apiResult = restClient.Execute <List <ProductRowApiO> >("Product", RestSharp.Method.GET, headerParameters: Headers);
                Assert.True(apiResult.Success, apiResult.Content);
                Assert.NotNull(apiResult.Result);

                using (MSSQLDbClient sqlClient = new MSSQLDbClient(ConnectionString))
                {
                    List <ProductRowApiO> sqlResult = sqlClient.Fill <List <ProductRowApiO> >("SELECT ProductID as ProductId, ProductName, SupplierID as SupplierId, CategoryID as CategoryId, QuantityPerUnit, "
                                                                                              + "UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued FROM Products ORDER BY ProductID");
                    Assert.NotNull(sqlResult);

                    Assert.True(apiResult.Result.Count == sqlResult.Count, "The record counts do not match.");
                }
            }
        }
        public void Login()
        {
            ApiLoginModel model = new ApiLoginModel()
            {
                Email = "*****@*****.**", Password = "******"
            };

            using (RESTClient restClient = new RESTClient(BaseUrl))
            {
                RestResult apiResult = restClient.Execute("Authorize/Login", RestSharp.Method.POST, jsonBody: model);

                Assert.True(apiResult.StatusCode == StatusCodes.Status200OK, "Logging in was not sucessful.");

                this.ApiToken  = apiResult.Headers.Find(h => h.Name == "ApiToken").Value.ToString();
                this.ApiCookie = apiResult.Cookies.Find(c => c.Name == AuthCookieName);

                Assert.True(this.ApiToken != string.Empty, "A token was not returned");
            }
        }
Beispiel #21
0
        public void GetOne()
        {
            using (RESTClient restClient = new RESTClient(BaseUrl))
            {
                List <KeyValuePair <string, string> > routeParams = new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("CategoryId", "2")
                };

                RestResult <CategoryRowApiO> apiResult = restClient.Execute <CategoryRowApiO>("Category/{CategoryId}", RestSharp.Method.POST, routeParameters: routeParams, headerParameters: Headers);
                Assert.True(apiResult.Success, apiResult.Content);
                Assert.NotNull(apiResult);

                using (MSSQLDbClient sqlClient = new MSSQLDbClient(ConnectionString))
                {
                    CategoryRowApiO sqlResult = sqlClient.Fill <CategoryRowApiO>("SELECT CategoryID as CategoryId, CategoryName, Description FROM Categories WHERE CategoryId = 2 ORDER BY CategoryID");
                    Assert.NotNull(sqlResult);
                    Assert.True(apiResult.Result.CategoryId == sqlResult.CategoryId && apiResult.Result.CategoryName == sqlResult.CategoryName, "The records do not match.");
                }
            }
        }
        public void GetOne()
        {
            using (RESTClient restClient = new RESTClient(BaseUrl))
            {
                List <KeyValuePair <string, string> > routeParams = new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("key", "2")
                };

                RestResult <ProductRowApiO> apiResult = restClient.Execute <ProductRowApiO>("Product/{key}", RestSharp.Method.POST, routeParameters: routeParams, headerParameters: Headers);
                Assert.True(apiResult.Success, apiResult.Content);
                Assert.NotNull(apiResult);

                using (MSSQLDbClient sqlClient = new MSSQLDbClient(ConnectionString))
                {
                    ProductRowApiO sqlResult = sqlClient.Fill <ProductRowApiO>("SELECT ProductID as ProductId, ProductName, SupplierID as SupplierId, CategoryID as CategoryId, QuantityPerUnit, "
                                                                               + "UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued FROM Products WHERE ProductId = 2 ORDER BY ProductID");
                    Assert.NotNull(sqlResult);
                    Assert.True(apiResult.Result.ProductId == sqlResult.ProductId && apiResult.Result.ProductName == sqlResult.ProductName, "The records do not match.");
                }
            }
        }
        public void Logout()
        {
            if (this.ApiToken == null || this.ApiCookie == null)
            {
                this.Login();
            }

            using (RESTClient restClient = new RESTClient(BaseUrl))
            {
                List <KeyValuePair <string, string> > headers = new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("ApiToken", this.ApiToken)
                };


                RestResult apiResult = restClient.Execute("Authorize/Logout", RestSharp.Method.GET, headerParameters: headers);

                this.ApiToken = string.Empty;

                Assert.True(apiResult.StatusCode == StatusCodes.Status200OK, "Logging out did not work.");
            }
        }
Beispiel #24
0
        static void Main(string[] args)
        {
            var exitEvent = new ManualResetEvent(false);
            var options   = new Options();

            if (CommandLine.Parser.Default.ParseArgumentsStrict(args, options, () => { Environment.Exit(-2); }))
            {
                if (options.RunAsServer)
                {
                    //
                    // As server
                    //
                    Console.CancelKeyPress += (sender, eventArgs) => {
                        eventArgs.Cancel = true;
                        exitEvent.Set();
                    };

                    Console.WriteLine("Run server on " + options.Host + ":" + options.Port);
                    Console.WriteLine("Press CTRL+C to terminate server.\n");
                    Console.WriteLine("Host: {0}:{1}", options.Host, options.Port);

                    try
                    {
                        LogManager.InstantiateLogManager();

                        DamIssuer.Init();

                        var server = new RESTServer();
                        server.Host = options.Host;
                        server.Port = options.Port;
                        server.Start();

                        exitEvent.WaitOne();
                        server.Stop();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message + "\n" + e.StackTrace);
                    }
                }
                else
                {
                    Dictionary <string, HttpMethod> method = new Dictionary <string, HttpMethod>()
                    {
                        { "GET", HttpMethod.GET },
                        { "POST", HttpMethod.POST }
                    };

                    //
                    // As client
                    //
                    try
                    {
                        LogManager.InstantiateLogManager();

                        RESTClient client = new RESTClient("http://" + options.Host + ":" + options.Port);

                        RESTRequest request = new RESTRequest(options.Url);
                        request.Method      = method[options.Method];
                        request.ContentType = ContentType.JSON;
                        request.Payload     = string.Format("{{\"file\":\"{0}\"}}", options.Json);
                        Console.WriteLine("Payload: " + request.Payload);

                        var response = client.Execute(request);
                        Console.WriteLine("Response: " + response.Content);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message + "\n" + e.StackTrace);
                    }
                }
            }
        }