Pact.io - 需要在请求中指定url子域

时间:2017-10-25 20:39:01

标签: android pact

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

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

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 / Path的模拟提供程序,因此无法在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();
  }

1 个答案:

答案 0 :(得分:0)

当您说&#34; URL子域&#34;指的是单独的Host标题,例如someaccount.foo.com或网址上的前缀,例如`http://foo.com/someaccount&#39;?

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