# 「テスト」ボタンの設定

GitBook では、いくつかの OpenAPI 拡張を使用して、「Test It」ボタンとそれに付随するウィンドウを設定できます。これらの拡張は、ユーザー向けのテストスイートの改善と設定に役立ちます。

### 「Test it」ボタンを非表示にする

エンドポイントに `x-hideTryItPanel` を追加するか、OpenAPI 仕様のルートに追加することで、エンドポイントから「Test it」ボタンを非表示にできます。

{% code title="openapi.yaml" %}

```yaml
openapi: '3.0'
info: ...
tags: [...]
paths:
  /example:
    get:
      summary: Example summary
      description: Example description
      operationId: examplePath
      responses: [...]
      parameters: [...]
      x-hideTryItPanel: true
```

{% endcode %}

### 「Test it」リクエストをプロキシする

一部の API は、CORS が原因でブラウザからのリクエストをブロックします。

Route **Test it** のトラフィックを GitBook 経由でルーティングするには、 `x-enable-proxy` を仕様に追加します。

参照 [OpenAPI proxy の使用](https://app.gitbook.com/s/NkEGS7hzeqa35sMXQZ4X/api-references/guides/using-openapi-proxy) 例をご覧ください。

### テストウィンドウで認証を有効にする

リクエストランナーが認証を表示して適用できるのは、仕様でそれが宣言されている場合のみです。スキームを `components.securitySchemes`の下で定義し、次に `security` を使ってグローバルに、または操作ごとに（グローバル設定を上書き）紐付けます。

#### 認証スキームを宣言する

以下は一般的なパターンです。YAML ではストレートクォートを使用してください。

{% tabs %}
{% tab title="HTTP Bearer（例: JWT）" %}

```yaml
openapi: '3.0.3'
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
```

{% endtab %}

{% tab title="ヘッダー内の API キー" %}

```yaml
openapi: '3.0.3'
components:
  securitySchemes:
    apiKeyAuth:
      type: apiKey
      in: header
      name: X-API-Key
```

{% endtab %}

{% tab title="OAuth2（authorizationCode）" %}

```yaml
openapi: '3.0.3'
components:
  securitySchemes:
    oauth2:
      type: oauth2
      flows:
        authorizationCode:
          authorizationUrl: 'https://auth.example.com/oauth/authorize'
          tokenUrl: 'https://auth.example.com/oauth/token'
          scopes:
            read:items: 'アイテムを読む'
            write:items: 'アイテムを書き込む'
```

{% endtab %}
{% endtabs %}

#### スキームをグローバルまたは操作ごとに適用する

{% tabs %}
{% tab title="グローバル" %}

```yaml
openapi: '3.0.3'
security:
  - bearerAuth: []
paths: ...
```

{% endtab %}

{% tab title="操作ごと" %}

```yaml
paths:
  /reports:
    get:
      summary: Get reports
      security:
        - apiKeyAuth: []
      responses:
        '200':
          description: OK
```

{% endtab %}
{% endtabs %}

### でエンドポイント URL を制御する `servers`

リクエストランナーは、 `servers` 配列で定義した URL をターゲットにします。1 つ以上のサーバーを宣言でき、変数でパラメータ化することもできます。

{% tabs %}
{% tab title="単一サーバー" %}

```yaml
openapi: '3.0.3'
servers:
  - url: https://instance.api.region.example.cloud
```

{% endtab %}

{% tab title="複数サーバー" %}

```yaml
servers:
  - url: https://api.example.com
    description: 本番環境
  - url: https://staging-api.example.com
    description: ステージング
```

{% endtab %}

{% tab title="サーバー変数" %}

```yaml
servers:
  - url: https://{instance}.api.{region}.example.cloud
    variables:
      instance:
        default: acme
        description: テナントまたはインスタンスのスラッグ
      region:
        default: eu
        enum:
          - eu
          - us
          - ap
        description: 地域別デプロイ
```

{% endtab %}

{% tab title="操作ごとのサーバー" %}

<pre class="language-yaml"><code class="lang-yaml"><strong>paths:
</strong>  /reports:
    get:
      summary: Get reports
      servers:
        - url: https://reports.api.example.com
      responses:
        '200':
          description: OK
</code></pre>

{% endtab %}
{% endtabs %}
