github.com/machinebox/remoto@v0.1.2-0.20191024144331-eff21a7d321f/console/api/main_test.go (about)

     1  package api
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"net/url"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/matryer/is"
    12  )
    13  
    14  func TestRenderTemplate(t *testing.T) {
    15  	is := is.New(t)
    16  	q := url.Values{}
    17  	q.Set("definition", readFile(t, "testdata/example.remoto.go"))
    18  	req, err := http.NewRequest(http.MethodPost, "/api/templates/remotohttp/client.go", strings.NewReader(q.Encode()))
    19  	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    20  	is.NoErr(err)
    21  	w := httptest.NewRecorder()
    22  	h := handleRenderTemplate()
    23  	h(w, req)
    24  	body := w.Body.String()
    25  
    26  	is.Equal(w.Code, http.StatusOK)
    27  	is.True(strings.Contains(body, "type GreeterClient struct"))
    28  	is.True(strings.Contains(body, "type GreetRequest struct"))
    29  	is.True(strings.Contains(body, "type GreetResponse struct"))
    30  }
    31  
    32  func readFile(t *testing.T, path string) string {
    33  	is := is.New(t)
    34  	b, err := ioutil.ReadFile(path)
    35  	is.NoErr(err)
    36  	return string(b)
    37  }