【问题标题】:Pact.io - Need to specify url subdomain in requestPact.io - 需要在请求中指定 url 子域
【发布时间】:2017-10-25 20:39:01
【问题描述】:

我是 Pact.io 的新手,正在尝试在我们的平台上设置合约测试。该应用程序的设置方式是,每个客户帐户都有自己的数据库架构,该架构直接与 URL 子域相关联。发出 API 请求时,除了授权标头之外,还必须提供该 URL 子域。我可以创建一个静态令牌来传递消费者测试,但是当 Pact 发送请求时,它不知道要使用哪个帐户。我没有看到将 URL 子域作为消费者测试的一部分传递的方法,并且不确定如何强制它在提供者端使用特定帐户?有什么想法吗?

这是契约日志。我们正在向用户发出请求,而不是返回 JSON 正文,我们得到一个 HTML 错误页面,即使状态代码返回 200。

I, [2017-10-25T12:39:24.344559 #91639]  INFO -- : Running example 'Verifying a pact between bridge_perform and bridge_learn Given one user a get request for learn users with GET /api/learner/users returns a response which has a matching body'
I, [2017-10-25T12:41:40.962186 #91639]  INFO -- : Sending GET request to path: "/api/learner/users" with headers: {"HTTP_AUTHORIZATION"=>"Basic xxxxxxxxxxxxxxxxxxxxx"}, see debug logs for body
D, [2017-10-25T12:41:40.962234 #91639] DEBUG -- : body :
I, [2017-10-25T12:41:40.977995 #91639]  INFO -- : Received response with status: 200, headers: {"Content-Type"=>"text/html", "ETag"=>"W/\"1bd857d3e20d3ed50aa6f48b5be15f42\"", "Cache-Control"=>"max-age=0, private, must-revalidate", "X-Request-Id"=>"8dd9a9bf-da21-44b1-8b6b-9de486a7e9ea", "X-Runtime"=>"0.007579", "Content-Length"=>"630"}, see debug logs for body
D, [2017-10-25T12:41:40.978049 #91639] DEBUG -- : body: <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Account Not Found</title>
    <link rel="stylesheet" href="/stylesheets/application.css" />
  </head>
  <body class="indigo-bg">
    <div role="main" class="large-content-area centered margin-t-xl text-center white">
      <h1 class="h1">Oh, snap!</h1>
      <p class="large">It looks like you've tried to access App without telling us which
  account you belong to.</p>
      <p class="large">To log in, try using your account's URL. Usually that looks
  something like https://myaccount.app.com.</p>
    </div>
  </body>
</html>

以下是其中一项消费者测试的示例。如您所见,我们创建了一个没有 URL/路径的模拟提供程序,因此没有地方在 URL 上提供指定所需帐户的子域。

public class AdminImportedUserId  {
  @Rule
  public PactProviderRuleMk2 mockProvider = new PactProviderRuleMk2("provider_app", PactSpecVersion.V2, this);

  @Pact(provider = "provider_app", consumer = "consumer_app")
  public RequestResponsePact createFragment(PactDslWithProvider builder) throws IOException {
    return builder
        .given("an admin with two imported users")
        .uponReceiving("a get request for admin imported user id")
        .path("/api/admin/users/imports/1")
        .method("GET")
        .headers(new ProviderClient().getHeaders())
        .willRespondWith()
        .status(200)
        .body("{\"meta\":{},\"linked\":{\"contexts\":[{\"id\":1,\"class\":\"Domain\"}]},\"imports\":[{\"id\":\"160\","
        + "\"context_id\":\"1\",\"user_name\":\"user10 Royer\",\"context_description\":\"Dev Environment\","
        + "\"context_type\":\"Domain\",\"completed\":17,\"total\":17,\"state\":\"complete\",\"new_user_count\":0,"
        + "\"restored_user_count\":0,\"updated_user_count\":17,\"deleted_user_count\":0,\"ignored_user_count\":0,"
        + "\"deported_user_count\":17,\"invalid_rows\":[],\"created_at\":\"2017-09-23T13:13:21.132-06:00\","
        + "\"user_id\":4078}]}")
        .toPact();
  }

【问题讨论】:

    标签: android pact


    【解决方案1】:

    当您说“URL 子域”时,指的是单独的 Host 标头,例如someaccount.foo.com 或 URL 上的前缀,例如`http://foo.com/someaccount'?

    无论哪种方式,这些都将作为 URL/路径或特定标头进入您的消费者测试。

    【讨论】:

    • 一个单独的主机,例如someaccount.foo.com 其中someaccount 是帐户的名称。但是,消费者测试没有指定 URL/路径。它创建了一个没有 url 并且用 Java 编写的模拟提供程序。我将编辑以上内容以包括消费者测试。
    • 有一种方法可以在请求在 java impl 中重放之前对其进行修改(您只需要在文档中四处寻找,因为我不确定它在哪里,不是java人)。您可以尝试在那里覆盖 Host 标头。
    • @molteninjabob 为什么不在.headers(...) 子句中添加主机名?主机名和 URL 不是一回事
    • 我能够按照您的建议进行操作,并通过标题传入主机:"headers": { "Authorization": "Basic cGFjdF91c2VyOnBhY3Rfc2VjcmV0", "Host": "specs.lvh.me:3000" } 但是,这似乎仍然不起作用。此时,设置中的后端肯定缺少某些东西,因为请求似乎包含所需的一切。我可以看到该帐户已被播种,但由于某种原因,当 pact 发出请求时,它并没有使用它。我对请求规范使用完全相同的设置,这可行,但不适用于 Pact。
    • 我不确定您是否可以在主机标头中传递端口。它是否与协议测试匹配相同的端口?在这一点上,我认为我们需要代码来重现
    猜你喜欢
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多