【问题标题】:Passing a bearer token in a 'Web Test' without Visual Studio?在没有 Visual Studio 的情况下在“Web 测试”中传递不记名令牌?
【发布时间】:2017-03-03 23:32:28
【问题描述】:

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

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

  1. connect/token 端点请求一个不记名令牌。
  2. api/resources 执行 GET,并在标头中使用不记名令牌(从上述请求中检索)。

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

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

这是没有传递令牌的 web 测试的样子:

<?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>

【问题讨论】:

    标签: visual-studio visual-studio-2015 fiddler azure-application-insights


    【解决方案1】:

    恭维James Davis的回答,如果您需要通过发布JSON来登录https://yourapp.com/auth/login

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

    首先对json进行base64编码:

    > 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 可用性检查

    【讨论】:

      【解决方案2】:

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

      {"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>
      

      【讨论】:

      • 效果很好。有没有办法在没有 VS 测试版的情况下编写这样的测试?据我所知,Fiddler 只能做录制和播放请求。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多