在没有 Visual Studio 的 'Web Test' 中传递不记名令牌?

Passing a bearer token in a 'Web Test' without Visual Studio?

我想在 Azure 的 Application Insights 可用性功能中导入“.webtest”。我没有 Visual Studio 的测试版,但 this MSDN article 建议使用 Fiddler 作为创建 Web 测试的另一种选择。

我需要在 REST 上执行 2 个请求 API:

  1. connect/token 端点请求不记名令牌。
  2. 使用 header.
  3. 中的不记名令牌(从上述请求中检索)在 api/resources 处执行 GET

这是典型的客户端凭据 OAuth 2 流程。

我似乎无法弄清楚如何使用 Fiddler 执行此操作。基本上我需要从请求 1 的响应 body 中提取一个值,并将其用作请求 2 中的 header 值。

这是没有通过令牌的网络测试的样子:

<?xml version="1.0" encoding="utf-8"?>
<TestCase Name="FiddlerGeneratedWebTest" Id="" Owner="" Description="" Priority="0" Enabled="True" CssProjectStructure="" CssIteration="" DeploymentItemsEditable="" CredentialUserName="" CredentialPassword="" PreAuthenticate="True" Proxy="" RequestCallbackClass="" TestCaseCallbackClass="">
  <Items>
    <Request Method="POST" Version="1.1" Url="https://example.com/connect/token" ThinkTime="8" Timeout="60" ParseDependentRequests="True" FollowRedirects="True" RecordResult="True" Cache="False" ResponseTimeGoal="0" Encoding="utf-8">
      <Headers>
        <Header Name="Content-Type" Value="application/x-www-form-urlencoded" />
      </Headers>
      <FormPostHttpBody ContentType="application/x-www-form-urlencoded">
        <FormPostParameter Name="client_id" Value="myclientid" UrlEncode="True" />
        <FormPostParameter Name="client_secret" Value="password123" UrlEncode="True" />
        <FormPostParameter Name="grant_type" Value="client_credentials" UrlEncode="True" />
        <FormPostParameter Name="scope" Value="myscopes" UrlEncode="True" />
      </FormPostHttpBody>
    </Request>
    <Request Method="GET" Version="1.1" Url="https://example.com/api/resources" ThinkTime="0" Timeout="60" ParseDependentRequests="True" FollowRedirects="True" RecordResult="True" Cache="False" ResponseTimeGoal="0" Encoding="utf-8">
      <Headers>
        <Header Name="Authorization" Value="Bearer {{token}}" />
      </Headers>
    </Request>
  </Items>
</TestCase>

假设这作为以下示例返回,您可以使用正则表达式提取来获取它。

{"token_type":"Bearer","scope":"user_impersonation","expires_in":"3600 ... "access_token":"{{TOKEN}}", ...}

<?xml version="1.0" encoding="utf-8"?>
<TestCase Name="FiddlerGeneratedWebTest" Id="" Owner="" Description="" Priority="0" Enabled="True" CssProjectStructure="" CssIteration="" DeploymentItemsEditable="" CredentialUserName="" CredentialPassword="" PreAuthenticate="True" Proxy="" RequestCallbackClass="" TestCaseCallbackClass="">
  <Items>
    <Request Method="POST" Version="1.1" Url="https://example.com/connect/token" ThinkTime="8" Timeout="60" ParseDependentRequests="True" FollowRedirects="True" RecordResult="True" Cache="False" ResponseTimeGoal="0" Encoding="utf-8">
      <ExtractionRules>
            <ExtractionRule Classname="Microsoft.VisualStudio.TestTools.WebTesting.Rules.ExtractRegularExpression, Microsoft.VisualStudio.QualityTools.WebTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" VariableName="token" DisplayName="Extract Regular Expression" Description="Extract text from the response matching a regular expression and place it into the test context.">
              <RuleParameters>
                <RuleParameter Name="RegularExpression" Value=".*&quot;access_token&quot;:&quot;([^&quot;]*)&quot;.*" />
                <RuleParameter Name="IgnoreCase" Value="True" />
                <RuleParameter Name="Required" Value="True" />
                <RuleParameter Name="Index" Value="0" />
                <RuleParameter Name="HtmlDecode" Value="True" />
                <RuleParameter Name="UseGroups" Value="True" />
              </RuleParameters>
            </ExtractionRule>
      </ExtractionRules>
      <Headers>
        <Header Name="Content-Type" Value="application/x-www-form-urlencoded" />
      </Headers>
      <FormPostHttpBody ContentType="application/x-www-form-urlencoded">
        <FormPostParameter Name="client_id" Value="myclientid" UrlEncode="True" />
        <FormPostParameter Name="client_secret" Value="password123" UrlEncode="True" />
        <FormPostParameter Name="grant_type" Value="client_credentials" UrlEncode="True" />
        <FormPostParameter Name="scope" Value="myscopes" UrlEncode="True" />
      </FormPostHttpBody>
    </Request>
    <Request Method="GET" Version="1.1" Url="https://example.com/api/resources" ThinkTime="0" Timeout="60" ParseDependentRequests="True" FollowRedirects="True" RecordResult="True" Cache="False" ResponseTimeGoal="0" Encoding="utf-8">
      <Headers>
        <Header Name="Authorization" Value="Bearer {{token}}" />
      </Headers>
    </Request>
  </Items>
</TestCase>

通过发布JSON赞美James Davis's answer, if you need to login to https://yourapp.com/auth/login

{
  user: 'youruser', 
  password: 'yourpassword'
}

首先base64编码json:

> echo "{user: 'youruser', password: 'yourpassword'}" | base64
e3VzZXI6ICd5b3VydXNlcicsIHBhc3N3b3JkOiAneW91cnBhc3N3b3JkJ30K

然后在 StringHttpBody 标签中传递这个 base64 值

<?xml version="1.0" encoding="utf-8"?>
<WebTest Name="login-healthcheck" Id="e91b6e1d-3fa0-475f-a18b-b694b463589c" Owner="" Priority="0" Enabled="True" CssProjectStructure="" CssIteration="" Timeout="0" WorkItemIds="" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010" Description="" CredentialUserName="" CredentialPassword="" PreAuthenticate="True" Proxy="default" StopOnError="False" RecordedResultFile="" ResultsLocale="">
  <Items>
    <Request Method="POST" Guid="ef9d1d00-5663-476a-a3cb-ccf49c4d2229" Version="1.1" Url="https://yourapp.com/auth/login" ThinkTime="8" Timeout="60" ParseDependentRequests="True" FollowRedirects="True" RecordResult="True" Cache="False" ResponseTimeGoal="0" Encoding="utf-8" ExpectedHttpStatusCode="0" ExpectedResponseUrl="" ReportingName="" IgnoreHttpStatusCode="False">
      <Headers>
        <Header Name="Content-Type" Value="application/json" />
      </Headers>
      <ExtractionRules>
        <ExtractionRule Classname="Microsoft.VisualStudio.TestTools.WebTesting.Rules.ExtractRegularExpression, Microsoft.VisualStudio.QualityTools.WebTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" VariableName="token" DisplayName="Extract Regular Expression" Description="Extract text from the response matching a regular expression and place it into the test context.">
          <RuleParameters>
            <RuleParameter Name="RegularExpression" Value=".*&quot;access_token&quot;:&quot;([^&quot;]*)&quot;.*" />
            <RuleParameter Name="IgnoreCase" Value="True" />
            <RuleParameter Name="Required" Value="True" />
            <RuleParameter Name="Index" Value="0" />
            <RuleParameter Name="HtmlDecode" Value="True" />
            <RuleParameter Name="UseGroups" Value="True" />
          </RuleParameters>
        </ExtractionRule>
      </ExtractionRules>
      <StringHttpBody ContentType="application/json" InsertByteOrderMark="False">e3VzZXI6ICd5b3VydXNlcicsIHBhc3N3b3JkOiAneW91cnBhc3N3b3JkJ30K</StringHttpBody>
    </Request>
    <Request Method="GET" Guid="d566422f-af74-47bf-90aa-0c66db6ef567" Version="1.1" Url="https://yourapp.com/api/v1/healthcheck" ThinkTime="0" Timeout="60" ParseDependentRequests="True" FollowRedirects="True" RecordResult="True" Cache="False" ResponseTimeGoal="0" Encoding="utf-8" ExpectedHttpStatusCode="0" ExpectedResponseUrl="" ReportingName="" IgnoreHttpStatusCode="False">
      <Headers>
        <Header Name="Authorization" Value="Bearer {{token}}" />
      </Headers>
    </Request>
  </Items>
</WebTest>

为我工作 Azure Application Insights 可用性检查