Example #1
0
func TestProcess_StartStop(t *testing.T) {
	path := pathToPhpFpm
	process := gophpfpm.NewProcess(path)
	process.SetDatadir(basepath + "/var")
	process.User = username
	process.SaveConfig(basepath + "/etc/test.startstop.conf")

	if err := process.Start(); err != nil {
		t.Errorf("unexpected error: %s", err.Error())
		return
	}

	go func() {
		// do something that needs phpfpm
		// ...
		time.Sleep(time.Millisecond * 50)
		if err := process.Stop(); err != nil {
			panic(err)
		}
	}()

	if err := process.Wait(); err != nil {
		t.Errorf("unexpected error: %#v", err.Error())
	}
}
Example #2
0
func ExampleProcess() {

	process := gophpfpm.NewProcess(pathToPhpFpm)

	// SetDatadir equals to running these 3 settings:
	// process.PidFile  = basepath + "/phpfpm.pid"
	// process.ErrorLog = basepath + "/phpfpm.error_log"
	// process.Listen   = basepath + "/phpfpm.sock"
	process.SetDatadir(basepath + "/var")
	process.User = username

	// save the config file to basepath + "/etc/php-fpm.conf"
	process.SaveConfig(basepath + "/etc/example.conf")
	process.Start()

	go func() {
		// do something that needs phpfpm
		// ...
		time.Sleep(time.Millisecond * 50)
		process.Stop()
	}()

	process.Wait()

	// Output:
}
Example #3
0
func TestNew(t *testing.T) {
	path := pathToPhpFpm
	process := gophpfpm.NewProcess(path)
	if want, have := path, process.Exec; want != have {
		t.Errorf("expected %#v, got %#v", want, have)
	}
}
Example #4
0
func TestProcess_SetPrefix(t *testing.T) {
	path := pathToPhpFpm
	process := gophpfpm.NewProcess(path)
	process.SetDatadir(basepath + "/var")
	if want, have := basepath+"/var/phpfpm.pid", process.PidFile; want != have {
		t.Errorf("expected %#v, got %#v", want, have)
	}
	if want, have := basepath+"/var/phpfpm.error_log", process.ErrorLog; want != have {
		t.Errorf("expected %#v, got %#v", want, have)
	}
	if want, have := basepath+"/var/phpfpm.sock", process.Listen; want != have {
		t.Errorf("expected %#v, got %#v", want, have)
	}
}
Example #5
0
func TestHandler(t *testing.T) {

	if phpfpmPath == "" {
		t.Logf("empty TEST_PHPFPM_PATH, skip test")
		return
	}
	if stat, err := os.Stat(phpfpmPath); os.IsNotExist(err) {
		t.Errorf("TEST_PHPFPM_PATH (%#v) not found", phpfpmPath)
		return
	} else if fmode := stat.Mode(); !fmode.IsRegular() {
		t.Errorf("TEST_PHPFPM_PATH (%#v) is not a regular file", phpfpmPath)
		return
	}

	exmpPath := examplePath()
	process := gophpfpm.NewProcess(phpfpmPath)
	process.SetDatadir(path.Join(exmpPath, "var"))
	process.User = username
	process.SaveConfig(path.Join(exmpPath, "etc", "test.handler.conf"))
	if err := process.Start(); err != nil {
		t.Errorf("unexpected error: %s", err.Error())
		return
	}
	defer process.Stop()

	// start the proxy handler
	network, address := process.Address()
	h := php.NewHandler(
		path.Join(exmpPath, "htdocs"),
		network, address)

	get := func(path string) (w *httptest.ResponseRecorder, err error) {
		r, err := http.NewRequest("GET", path, nil)
		if err != nil {
			return
		}
		w = httptest.NewRecorder()
		h.ServeHTTP(w, r)
		return
	}

	post := func(path string, payload string) (w *httptest.ResponseRecorder, err error) {
		var reader io.Reader
		reader = strings.NewReader(payload)
		r, err := http.NewRequest("POST", path, reader)
		r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
		r.Header.Add("Content-Length", fmt.Sprintf("%d", len(payload)))
		if err != nil {
			return
		}
		w = httptest.NewRecorder()
		h.ServeHTTP(w, r)
		return
	}

	// check results
	w, err := get("/")
	if err != nil {
		t.Errorf("unexpected error %v", err)
		return
	}
	if want, have := "hello index", w.Body.String(); want != have {
		t.Errorf("expected %#v, got %#v", want, have)
	}

	w, err = get("/index.php")
	if err != nil {
		t.Errorf("unexpected error %v", err)
		return
	}
	if want, have := "hello index", w.Body.String(); want != have {
		t.Errorf("expected %#v, got %#v", want, have)
	}

	w, err = get("/form.php")
	if err != nil {
		t.Errorf("unexpected error %v", err)
		return
	}
	formPrefix := "<!DOCTYPE html>\n<html>\n<head>\n  <title>Simple Form"
	if have := w.Body.String(); !strings.HasPrefix(have, formPrefix) {
		t.Errorf("expected to start with %#v, got %#v", formPrefix, have)
	}

	w, err = get("/form.php?hello=world")
	if err != nil {
		t.Errorf("unexpected error %v", err)
		return
	}
	if want, have := "$_GET = array (\n  'hello' => 'world',\n)", w.Body.String(); want != have {
		t.Errorf("expected %#v, got %#v", want, have)
	}

	form := url.Values{}
	form.Add("text_input", "hello world")
	w, err = post("/form.php", form.Encode())
	if err != nil {
		t.Errorf("unexpected error %v", err)
		return
	}
	if want, have := "$_POST = array (\n  'text_input' => 'hello world',\n)", w.Body.String(); want != have {
		t.Errorf("expected %#v, got %#v", want, have)
	}

}