...

Source file src/github.com/go-openapi/loads/options_test.go

Documentation: github.com/go-openapi/loads

     1  package loads
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/go-openapi/swag"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  const optionFixture = "fixtures/json/resources/pathLoaderIssue.json"
    16  
    17  func TestOptionsWithDocLoader(t *testing.T) {
    18  	document, err := Spec(optionFixture, WithDocLoader(func(pth string) (json.RawMessage, error) {
    19  		buf, err := os.ReadFile(pth)
    20  		return json.RawMessage(buf), err
    21  	}))
    22  	require.NoError(t, err)
    23  	require.NotNil(t, document)
    24  	require.NotNil(t, document.pathLoader)
    25  
    26  	b, err := document.pathLoader.Load(optionFixture)
    27  	require.NoError(t, err)
    28  
    29  	trimmed, err := trimData(b)
    30  	require.NoError(t, err)
    31  
    32  	assert.EqualValues(t, trimmed, document.Raw())
    33  
    34  	// a nil loader is a no op
    35  	_, err = Spec(optionFixture, WithDocLoader(nil))
    36  	require.NoError(t, err)
    37  }
    38  
    39  func TestOptionsLoaderFromOptions(t *testing.T) {
    40  	var called int
    41  
    42  	// not chaining here, just replacing with the last one
    43  	l := loaderFromOptions([]LoaderOption{
    44  		WithDocLoader(func(pth string) (json.RawMessage, error) {
    45  			called = 1
    46  			buf, err := os.ReadFile(pth)
    47  			return json.RawMessage(buf), err
    48  		}),
    49  		WithDocLoader(func(pth string) (json.RawMessage, error) {
    50  			called = 2
    51  			buf, err := os.ReadFile(pth)
    52  			return json.RawMessage(buf), err
    53  		}),
    54  	})
    55  	require.NotNil(t, l)
    56  
    57  	b, err := l.Load(optionFixture)
    58  	require.NoError(t, err)
    59  	require.NotNil(t, b)
    60  
    61  	require.Equal(t, 2, called)
    62  }
    63  
    64  func TestOptionsWithDocLoaderMatches(t *testing.T) {
    65  	jsonLoader := NewDocLoaderWithMatch(
    66  		func(pth string) (json.RawMessage, error) {
    67  			buf, err := os.ReadFile(pth)
    68  			return json.RawMessage(buf), err
    69  		},
    70  		func(pth string) bool {
    71  			return filepath.Ext(pth) == ".json"
    72  		},
    73  	)
    74  
    75  	document, err := Spec(optionFixture, WithDocLoaderMatches(jsonLoader))
    76  	require.NoError(t, err)
    77  	require.NotNil(t, document)
    78  	require.NotNil(t, document.pathLoader)
    79  
    80  	yamlLoader := NewDocLoaderWithMatch(
    81  		swag.YAMLDoc,
    82  		func(pth string) bool {
    83  			return filepath.Ext(pth) == ".yaml"
    84  		},
    85  	)
    86  
    87  	document, err = Spec(optionFixture, WithDocLoaderMatches(yamlLoader))
    88  	require.Error(t, err)
    89  	require.Nil(t, document)
    90  
    91  	// chained loaders, with different ordering
    92  	document, err = Spec(optionFixture, WithDocLoaderMatches(yamlLoader, jsonLoader))
    93  	require.NoError(t, err)
    94  	require.NotNil(t, document)
    95  
    96  	document, err = Spec(optionFixture, WithDocLoaderMatches(jsonLoader, yamlLoader))
    97  	require.NoError(t, err)
    98  	require.NotNil(t, document)
    99  
   100  	// the nil loader is a no op
   101  	nilLoader := NewDocLoaderWithMatch(nil, nil)
   102  	document, err = Spec(optionFixture, WithDocLoaderMatches(nilLoader, jsonLoader, yamlLoader))
   103  	require.NoError(t, err)
   104  	require.NotNil(t, document)
   105  
   106  	// the nil matcher always matches
   107  	nilMatcher := NewDocLoaderWithMatch(func(_ string) (json.RawMessage, error) {
   108  		return nil, errors.New("test")
   109  	}, nil)
   110  	_, err = Spec(optionFixture, WithDocLoaderMatches(nilMatcher))
   111  	require.Error(t, err)
   112  	require.Equal(t, "test", err.Error())
   113  
   114  	// when a matcher returns an errors, the next one is tried
   115  	document, err = Spec(optionFixture, WithDocLoaderMatches(nilMatcher, jsonLoader, yamlLoader))
   116  	require.NoError(t, err)
   117  	require.NotNil(t, document)
   118  }
   119  

View as plain text