github.com/w3security/vervet/v5@v5.3.1-0.20230618081846-5bd9b5d799dc/ref_alias_resolver_test.go (about)

     1  package vervet_test
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"os"
     7  	"testing"
     8  	"time"
     9  
    10  	qt "github.com/frankban/quicktest"
    11  
    12  	"github.com/w3security/vervet/v5"
    13  	"github.com/w3security/vervet/v5/testdata"
    14  )
    15  
    16  func TestLocalize(t *testing.T) {
    17  	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
    18  	defer cancel()
    19  	c := qt.New(t)
    20  	doc, err := vervet.NewDocumentFile(testdata.Path("resources/_examples/hello-world/2021-06-01/spec.yaml"))
    21  	c.Assert(err, qt.IsNil)
    22  	err = vervet.Localize(ctx, doc)
    23  	c.Assert(err, qt.IsNil)
    24  	err = doc.Validate(context.TODO())
    25  	c.Assert(err, qt.IsNil)
    26  
    27  	// OpenAPI DOM should be fully localized and relocatable now.
    28  	yamlBuf, err := vervet.ToSpecYAML(doc)
    29  	c.Assert(err, qt.IsNil)
    30  	tmpDir := c.TempDir()
    31  	err = os.WriteFile(tmpDir+"/spec.yaml", yamlBuf, 0644)
    32  	c.Assert(err, qt.IsNil)
    33  
    34  	// This will fail to load if references have not been localized!
    35  	doc2, err := vervet.NewDocumentFile(tmpDir + "/spec.yaml")
    36  	c.Assert(err, qt.IsNil)
    37  	c.Assert(doc2.Validate(context.TODO()), qt.IsNil)
    38  
    39  	// Assert round-trip serialization equality
    40  	jsonBuf, err := json.Marshal(doc)
    41  	c.Assert(err, qt.IsNil)
    42  	c.Assert(jsonBuf, qt.JSONEquals, doc2)
    43  }