Example #1
0
// docker run golang go version
func TestRunContainer(t *testing.T) {
	var payload = []byte(`{"AttachStdin": "false",
		"AttachStout": "true",
		"AttachStderr": "true",
		"Tty": "false",
		"OpenStdin": "false",
		"StdinOnce": "false",
		"Cmd":["/bin/bash", "-c", "go", "version"],
		"Image": "golang",
		"DisableNetwork": "false"
	}`)

	mockReq, _ := http.NewRequest("POST", "/containers/create?name=golang", bytes.NewBuffer(payload))

	ctx := context.Empty()
	ctx.SetHttpRequest(mockReq)

	modDocker := NewDocker(defaultParams())
	resp, err := modDocker.ProcessRequest(ctx)
	assert.Nil(t, err)

	assert.Equal(t, resp.StatusCode, 200)
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Printf("Docker Run output\n============\n %s\n", body)
}
Example #2
0
func TestHappyProcessChain(t *testing.T) {
	OnRequest1Fn := func(ctx context.ContextSpec) (*http.Response, error) {
		return nil, nil
	}
	OnResponse1Fn := func(ctx context.ContextSpec) (*http.Response, error) {
		return nil, nil
	}

	OnRequest2Fn := func(ctx context.ContextSpec) (*http.Response, error) {
		return httputils.NewTextResponse(nil, 200, "happy life"), nil
	}
	OnResponse2Fn := func(ctx context.ContextSpec) (*http.Response, error) {
		return nil, nil
	}

	modMock1 := modules.NewWithParams("mod_mock_1", OnRequest1Fn, OnResponse1Fn)
	modMock2 := modules.NewWithParams("mod_mock_2", OnRequest2Fn, OnResponse2Fn)

	chain := New()
	chain.AppendModule(modMock1)
	chain.AppendModule(modMock2)

	assert.Equal(t, len(chain.GetModules()), 2)

	resp, err := chain.Process(context.Empty())

	assert.Nil(t, err)
	assert.Equal(t, resp.StatusCode, 200)
}
Example #3
0
func TestGetImages(t *testing.T) {
	// Prepare the context
	mockReq, _ := http.NewRequest("GET", "/images/json", nil)
	ctx := context.Empty()
	ctx.SetHttpRequest(mockReq)

	modDocker := NewDocker(defaultParams())
	resp, err := modDocker.ProcessRequest(ctx)

	assert.Nil(t, err)
	assert.Equal(t, resp.StatusCode, 200)
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Printf("Docker Images\n============\n %s\n", body)
}
Example #4
0
func TestChangePath(t *testing.T) {

	mockReq, _ := http.NewRequest("GET", "/docker/images/json?query=example", nil)
	ctx := context.Empty()
	ctx.SetHttpRequest(mockReq)

	modRewrite := NewRewrite(defaultParams())
	_, err := modRewrite.ProcessRequest(ctx)

	assert.Nil(t, err)
	//assert.Equal(t, resp.StatusCode, 200)
	//body, _ := ioutil.ReadAll(resp.Body)
	//fmt.Printf("Body\n============\n %s\n", body)
}
Example #5
0
func TestHappyRead(t *testing.T) {
	// register the modules
	modprobe.Install("mod_mock", NewModMock)

	input := []byte(`
	{"id": "getping",
     "method": "GET",
     "enabled": true,
     "pattern": "/ping/:hola",
     "chain": [
  	    {"ref": "mod_mock", 
  	     "params": {
  	     	"optiona1": "a1", 
  	     	"optiona2": "a2"}},
	    {"ref": "mod_mock", 
	     "params": {
	     	"optiona1": "b1"}
	     }
	     ]
	}`)
	p, err := newProxyFromJson(input)
	assert.Nil(t, err)
	assert.Equal(t, p.GetId(), "getping")
	assert.Equal(t, p.GetMethod(), "GET")
	assert.Equal(t, p.IsEnabled(), true)
	assert.Equal(t, p.GetPattern(), "/ping/:hola")
	assert.Equal(t, len(p.GetChain().GetModules()), 2)

	modType := reflect.TypeOf(p.GetChain().GetModules()[0])

	fmt.Printf("All: %+v\n", modType)
	fmt.Printf("Name: %s\n", modType.Name())
	fmt.Printf("Pkg: %s\n", modType.PkgPath())

	fmt.Printf("Kind: %s\n", modType.Kind())
}
Example #6
0
func TestHappyProxy(t *testing.T) {
	proxy := New("testProxy", "GET", "/ping", true, getMockChain())
	resp, err := proxy.RoundTrip(context.Empty())
	assert.Nil(t, err)
	assert.Equal(t, resp.StatusCode, 200)
}