github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/hugolib/openapi_test.go (about)

     1  // Copyright 2019 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package hugolib
    15  
    16  import (
    17  	"strings"
    18  	"testing"
    19  )
    20  
    21  func TestOpenAPI3(t *testing.T) {
    22  	const openapi3Yaml = `openapi: 3.0.0
    23  info:
    24    title: Sample API
    25    description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.
    26    version: 0.1.9
    27  servers:
    28    - url: http://api.example.com/v1
    29      description: Optional server description, e.g. Main (production) server
    30    - url: http://staging-api.example.com
    31      description: Optional server description, e.g. Internal staging server for testing
    32  paths:
    33    /users:
    34      get:
    35        summary: Returns a list of users.
    36        description: Optional extended description in CommonMark or HTML.
    37        responses:
    38          '200':    # status code
    39            description: A JSON array of user names
    40            content:
    41              application/json:
    42                schema: 
    43                  type: array
    44                  items: 
    45                    type: string
    46  `
    47  
    48  	b := newTestSitesBuilder(t).Running()
    49  	b.WithSourceFile("assets/api/myapi.yaml", openapi3Yaml)
    50  
    51  	b.WithTemplatesAdded("index.html", `
    52  {{ $api := resources.Get "api/myapi.yaml" | openapi3.Unmarshal }}
    53  
    54  API: {{ $api.Info.Title | safeHTML }}
    55  
    56  
    57  `)
    58  
    59  	b.Build(BuildCfg{})
    60  
    61  	b.AssertFileContent("public/index.html", `API: Sample API`)
    62  
    63  	b.EditFiles("assets/api/myapi.yaml", strings.Replace(openapi3Yaml, "Sample API", "Hugo API", -1))
    64  
    65  	b.Build(BuildCfg{})
    66  
    67  	b.AssertFileContent("public/index.html", `API: Hugo API`)
    68  }