Exemple #1
0
        public void Initialize()
        {
            Proxy = new Proxy(10011);
            Proxy.DeleteAll();

            // Post
            var s = SimulationFactory.When.I.Post().ToImdb("/Pet/{Kind}/Toy/{Name}/Price", "owner").AsJson().And.NotExists("{Ranking}").Then.AddToImdb().And.Return.StatusCode(System.Net.HttpStatusCode.Created);
            Proxy.Add(s);

            s = SimulationFactory.When.I.Post().ToImdb("/Pet/{Kind}/Toy/{Name}/Price", "owner").AsJson().And.Exists("{Ranking}").Then.Return.StatusCode(System.Net.HttpStatusCode.Conflict);
            Proxy.Add(s);

            // Put
            s = SimulationFactory.When.I.Put().ToImdb("/Pet/{Kind}/Toy/{Name}/Price", "owner").AsJson().And.NotExists("{Ranking}").Then.Return.StatusCode(System.Net.HttpStatusCode.Created).Then.AddToImdb();
            Proxy.Add(s);

            s = SimulationFactory.When.I.Put().ToImdb("/Pet/{Kind}/Toy/{Name}/Price", "owner").AsJson().And.Exists("{Ranking}").Then.Return.StatusCode(System.Net.HttpStatusCode.PartialContent).Then.AddToImdb();
            Proxy.Add(s);

            s = SimulationFactory.When.I.Put().ToImdb("/Pet/{Kind}/Toy/{Name}/Price/{Ranking}", "owner").AsJson().And.NotExists("{Ranking}").Then.Return.StatusCode(System.Net.HttpStatusCode.Unauthorized).Then.AddToImdb();
            Proxy.Add(s);

            s = SimulationFactory.When.I.Put().ToImdb("/Pet/{Kind}/Toy/{Name}/Price/{Ranking}", "owner").AsJson().And.Exists("{Ranking}").Then.Return.StatusCode(System.Net.HttpStatusCode.UnsupportedMediaType).Then.AddToImdb();
            Proxy.Add(s);

            // GetById
            s = SimulationFactory.When.I.Get().FromImdb("/Pet/{Kind}/Toy/{Name}/Price/{Ranking}", "owner").AsJson().And.Exists("{Ranking}").Then.Return.StatusCode(System.Net.HttpStatusCode.OK).And.Body("{value}");
            Proxy.Add(s);

            s = SimulationFactory.When.I.Get().FromImdb("/Pet/{Kind}/Toy/{Name}/Price/{Ranking}", "owner").AsJson().And.NotExists("{Ranking}").Then.Return.StatusCode(System.Net.HttpStatusCode.NotFound);
            Proxy.Add(s);

            // Get All
            s = SimulationFactory.When.I.Get().FromImdb("/Pet/{Kind}/Toy/{Name}/Price", "owner").AsJson().Then.Return.StatusCode(System.Net.HttpStatusCode.PaymentRequired).And.Body("[{value}]");
            Proxy.Add(s);
        }
Exemple #2
0
        static void Main(string[] args)
        {
            if (args == null || args.Length < 1)
            {
                Usage();
                System.Environment.Exit(1);
            }

            int port = 0;
            var success = Int32.TryParse(args[0], out port);
            if (!success)
            {
                Usage();
                System.Environment.Exit(1);
            }

            bool simulationsSpecified = false;
            string simulationsPath = null;
            Moksy.Common.SimulationCollection simulations = new Common.SimulationCollection();
            simulationsPath = args.FirstOrDefault(f => f.StartsWith("/File:", StringComparison.InvariantCultureIgnoreCase));
            if (simulationsPath != null)
            {
                simulationsPath = simulationsPath.Substring(6);
                simulationsSpecified = true;
            }
            var log = args.FirstOrDefault(f => f.StartsWith("/Log:true", StringComparison.InvariantCultureIgnoreCase) || string.Compare(f, "/log", true) == 0);

            System.Reflection.Assembly thisExe;
            thisExe = System.Reflection.Assembly.GetExecutingAssembly();
            var list = thisExe.GetManifestResourceNames();

            var header = ReadResource("Moksy.Host.Resources.Header.txt");
            var simulationsText = ReadResource("Moksy.Host.Resources.Simulations.txt");

            ApplicationDirectives parameters = new ApplicationDirectives() { Log = (log != null) };

            try
            {
                Console.WriteLine("----------------------------------------------");
                Console.WriteLine("MOKSY: REST API / JSON Endpoint Faking Toolkit");
                Console.WriteLine("----------------------------------------------");
                Console.WriteLine("by Grey Ham");
                Console.WriteLine();
                Console.WriteLine("See www.brekit.com for more information. ");
                Console.WriteLine("Source at https://github.com/greyham/Moksy");
                Console.WriteLine();

                if (simulationsSpecified)
                {
                    Console.Write(string.Format("Loading simulations from {0}...", simulationsPath));

                    var contents = System.IO.File.ReadAllText(simulationsPath);
                    simulations = Newtonsoft.Json.JsonConvert.DeserializeObject<Moksy.Common.SimulationCollection>(contents);

                    Console.WriteLine(string.Format("{0} simulations have been loaded. ", simulations.Count));
                    Console.WriteLine("");

                    // ASSERTION: We have loaded the simulations into memory.
                }

                Application app = new Application(port, parameters);
                app.Start();

                Console.WriteLine(string.Format("Running Moksy on Port {0}. ", port));
                Console.WriteLine(string.Format("Navigate to: http://localhost:{0} for a sanity test.", port));

                if (simulationsSpecified)
                {
                    Task.Factory.StartNew(() =>
                    {
                        Proxy proxy = new Proxy(port);
                        proxy.Wait(20);

                        // We need to wait until the service has started.
                        foreach (var simulation in simulations)
                        {
                            proxy.Add(simulation);
                        }
                    }, TaskCreationOptions.LongRunning
                    );
                }

                Console.WriteLine("Press a key to exit...");
                Console.ReadKey();
                app.Stop();
            }
            catch (System.AggregateException aggregate)
            {
                Console.WriteLine("ERROR: Launching Moksy.Host.Exe");
                Console.WriteLine(aggregate.Message);
                if (aggregate.InnerException.GetType().FullName == "System.ServiceModel.AddressAccessDeniedException")
                {
                    Console.WriteLine();
                    Console.WriteLine("Moksy launches a real HTTP Server and opens up a HTTP Endpoint so that your");
                    Console.WriteLine("tests and other services can hit it. ");
                    Console.WriteLine();
                    Console.WriteLine("By default, only applications launched as Administrator are allowed to");
                    Console.WriteLine("launch the HTTP Server and create the end-point. ");
                    Console.WriteLine();
                    Console.WriteLine("You are not running as Administrator. ");
                    Console.WriteLine();
                    Console.WriteLine("There are two ways to proceed:");
                    Console.WriteLine();
                    Console.WriteLine("1. Launch Moksy.Host.Exe as Administrator. ");
                    Console.WriteLine();
                    Console.WriteLine("2. Or perform the following operation once from an Administrator Command Prompt");
                    Console.WriteLine("   to create the reservation that will allow everyone to launch a HTTP Server");
                    Console.WriteLine("   and open up the end-point: ");
                    Console.WriteLine();
                    Console.WriteLine(string.Format(@"      netsh http add urlacl url=http://+:{0}/ sddl=""D:(A;;GX;;;WD)""", port));
                    Console.WriteLine();
                    Console.WriteLine("   Then re-run Moksy.Host.Exe as an ordinary user. ");
                    Console.WriteLine();
                    System.Environment.Exit(123);
                }

                System.Environment.Exit(1);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: Launching Moksy.Host.Exe");
                Console.WriteLine(ex.Message);
                System.Environment.Exit(1);
            }
        }