github.com/vyskocilm/postkid/builder@v0.0.0-20191001131220-20c88b283fad/lib_test.go (about)

     1  package builder
     2  
     3  import (
     4  	"go/parser"
     5  	"go/token"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  const yml = `method: GET
    15  host: http://example.com
    16  part: /path
    17  query:
    18    foo: bar
    19  header:
    20    ham: spam
    21  `
    22  
    23  func TestCurl(t *testing.T) {
    24  	assert := assert.New(t)
    25  
    26  	req, err := ParseString(yml)
    27  	assert.NoError(err)
    28  
    29  	var sb strings.Builder
    30  
    31  	b := New(
    32  		WithCurl("cURL.exe"),
    33  		WithOutputWriter(&sb),
    34  	)
    35  	err = b.Curl(&req)
    36  	assert.NoError(err)
    37  	assert.Equal(
    38  		"cURL.exe -XGET -H 'ham: spam' 'http://example.com/?foo=bar' ",
    39  		sb.String())
    40  }
    41  
    42  func TestGo(t *testing.T) {
    43  	assert := assert.New(t)
    44  	require := require.New(t)
    45  
    46  	var sb strings.Builder
    47  
    48  	b := New(
    49  		WithCurl("cURL.exe"),
    50  		WithOutputWriter(&sb),
    51  	)
    52  	req := Request{
    53  		Method: "GET",
    54  		Query:  map[string]string{"foo": "bar"},
    55  	}
    56  	err := b.Go(&req)
    57  	assert.NoError(err)
    58  
    59  	cwd, err := os.Getwd()
    60  	require.NoError(err)
    61  	err = os.Chdir("testdata")
    62  	require.NoError(err)
    63  
    64  	fset := token.NewFileSet() // positions are relative to fset
    65      f, err := parser.ParseFile(fset, "", sb.String(), parser.AllErrors)
    66  	assert.NoError(err)
    67  
    68      // XXX: this is included only to know ast test works
    69      if testing.Verbose() {
    70          t.Logf(sb.String())
    71          // Print the imports from the file's AST.
    72          for _, s := range f.Imports {
    73              t.Logf(s.Path.Value)
    74          }
    75      }
    76  
    77  	err = os.Chdir(cwd)
    78  	require.NoError(err)
    79  }