Exemplo n.º 1
0
func TestEndpointUnchanged(t *testing.T) {

	var requestContent string

	expectedResult := `test`
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		req, _ := ioutil.ReadAll(r.Body)
		requestContent = string(req)
		w.Write([]byte(expectedResult))
	}))
	defer ts.Close()

	conf := rapi.NewConfig()
	conf.Backend.Address = strings.TrimPrefix(ts.URL, "http://")

	api := rapi.New(conf)
	require.NotNil(t, api)

	ep := rapi.NewEndpoint(api, "POST", "/foo")
	assert.NotNil(t, ep)

	fixture := `{"One":"this is the one", "Two":"this is the second"}`
	req, err := http.NewRequest("POST", "/foo", strings.NewReader(fixture))
	require.Nil(t, err)
	require.NotNil(t, req)

	resp := httptest.NewRecorder()
	require.NotNil(t, resp)

	ep.ServeHTTP(resp, req)

	assert.Equal(t, http.StatusOK, resp.Code)
	assert.Equal(t, fixture, requestContent, "request body is unchanged")
}
Exemplo n.º 2
0
func TestCopyUrlVars(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
	defer ts.Close()

	conf := rapi.NewConfig()
	conf.Backend.Address = strings.TrimPrefix(ts.URL, "http://")

	api := rapi.New(conf)
	ep := rapi.NewEndpoint(api, "GET", "/{Foo}/{Bar}/{Baz}").
		InternalPath("/{{.Baz}}/{{.Bar}}/{{.Foo}}")

	req, _ := http.NewRequest("GET", "/foo/bar/baz", nil)
	res := httptest.NewRecorder()

	ep.CopyUrlVars(req)
	api.Router().ServeHTTP(res, req)

	s := reflect.ValueOf(ep).Elem()
	assert.Equal(t, "/baz/bar/foo", s.FieldByName("path").String())
}
Exemplo n.º 3
0
func main() {
	var configPath string

	flag.StringVar(&configPath, "c", "config.json", "Path to config file")
	flag.Parse()
	flag.Visit(func(v *flag.Flag) {
		fmt.Printf("%s - %s: %s\n", v.Usage, v.Name, v.Value)
	})

	config := rapi.NewConfigFile(configPath)
	api := rapi.New(config)

	api.NewEndpoint("POST", "/auth/login")
	api.NewEndpoint("GET", "/users")
	api.NewEndpoint("GET", "/invoices").
		TransformResponse(&InvoiceInternal{}, &InvoiceExternal{})
	api.NewEndpoint("GET", "/invoices/{Id:[A-Z0-9]+}").
		InternalPath("/invoices/{{.Id}}").
		TransformResponse(&InvoiceInternal{}, &InvoiceExternal{})

	api.Run()
}
Exemplo n.º 4
0
func TestCreateApi(t *testing.T) {
	config := rapi.NewConfig()
	rapi.New(config)
}