コード例 #1
0
        /// <summary>
        /// Common helper method for other tests to create a context.
        /// </summary>
        /// <param name="contextVariable">The variable name that will hold the new context.</param>
        public static void CreateServerContextSqlAuth(
            System.Management.Automation.PowerShell powershell,
            string contextVariable)
        {
            HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession(
                "UnitTest.Common.NewAzureSqlDatabaseServerContextWithSqlAuth");

            DatabaseTestHelper.SetDefaultTestSessionSettings(testSession);
            testSession.RequestValidator =
                new Action <HttpMessage, HttpMessage.Request>(
                    (expected, actual) =>
            {
                Assert.AreEqual(expected.RequestInfo.Method, actual.Method);
                Assert.AreEqual(expected.RequestInfo.UserAgent, actual.UserAgent);
                switch (expected.Index)
                {
                // Request 0-2: Create context with both ManageUrl and ServerName overriden
                case 0:
                    // GetAccessToken call
                    DatabaseTestHelper.ValidateGetAccessTokenRequest(
                        expected.RequestInfo,
                        actual);
                    break;

                case 1:
                    // Get server call
                    DatabaseTestHelper.ValidateHeadersForODataRequest(
                        expected.RequestInfo,
                        actual);
                    break;

                case 2:
                    // $metadata call
                    Assert.IsTrue(
                        actual.RequestUri.AbsoluteUri.EndsWith("$metadata"),
                        "Incorrect Uri specified for $metadata");
                    DatabaseTestHelper.ValidateHeadersForServiceRequest(
                        expected.RequestInfo,
                        actual);
                    Assert.AreEqual(
                        expected.RequestInfo.Headers[DataServiceConstants.AccessTokenHeader],
                        actual.Headers[DataServiceConstants.AccessTokenHeader],
                        "AccessToken header does not match");
                    Assert.AreEqual(
                        expected.RequestInfo.Cookies[DataServiceConstants.AccessCookie],
                        actual.Cookies[DataServiceConstants.AccessCookie],
                        "AccessCookie does not match");
                    break;

                default:
                    Assert.Fail("No more requests expected.");
                    break;
                }
            });

            UnitTestHelper.ImportAzureModule(powershell);
            UnitTestHelper.CreateTestCredential(
                powershell,
                testSession.SessionProperties["Username"],
                testSession.SessionProperties["Password"]);

            Collection <PSObject> serverContext;

            using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
            {
                using (new MockHttpServer(
                           exceptionManager,
                           MockHttpServer.DefaultServerPrefixUri,
                           testSession))
                {
                    serverContext = powershell.InvokeBatchScript(
                        string.Format(
                            CultureInfo.InvariantCulture,
                            @"{1} = New-AzureSqlDatabaseServerContext " +
                            @"-ServerName {2} " +
                            @"-ManageUrl {0} " +
                            @"-Credential $credential",
                            MockHttpServer.DefaultServerPrefixUri.AbsoluteUri,
                            contextVariable,
                            testSession.SessionProperties["Servername"]),
                        contextVariable);
                }
            }

            Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!");
            Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!");
            powershell.Streams.ClearStreams();

            PSObject contextPsObject = serverContext.Single();

            Assert.IsTrue(
                contextPsObject.BaseObject is ServerDataServiceSqlAuth,
                "Expecting a ServerDataServiceSqlAuth object");
        }