示例#1
0
func TestBuildTargetRequestURL_NormalURL(t *testing.T) {
	expectedURL := "http://google.se/path"
	mockhandler := handler.NewMockhandler("google.se", "/dir/test")
	req, err := http.NewRequest("GET", "http://myservice.com/path", strings.NewReader(string("body")))
	if err != nil {
		t.Errorf("%v", err)
	}

	url := mockhandler.BuildTargetRequestURL(req)

	if url != expectedURL {
		t.Errorf("Expected: %v but got: %v", expectedURL, url)
	}
}
示例#2
0
func TestBuildTargetRequestURL_URLWithQuery(t *testing.T) {
	expectedURL := "http://google.se/example/query?value=abc&value2=cba"
	mockhandler := handler.NewMockhandler("google.se", "/dir/test")
	req, err := http.NewRequest("GET", "http://myservice.com/example/query?value=abc&value2=cba", strings.NewReader(string("body")))
	if err != nil {
		t.Errorf("%v", err)
	}

	url := mockhandler.BuildTargetRequestURL(req)

	if url != expectedURL {
		t.Errorf("Expected: %v but got: %v", expectedURL, url)
	}
}
示例#3
0
func TestBuildTargetRequestURL_URLWithPortSpecified(t *testing.T) {
	expectedURL := "http://localhost:8080/path"
	mockhandler := handler.NewMockhandler("localhost:8080", "/dir/test")
	req, err := http.NewRequest("GET", "http://myservice.com/path", strings.NewReader(string("body")))
	if err != nil {
		t.Errorf("%v", err)
	}

	url := mockhandler.BuildTargetRequestURL(req)

	if url != expectedURL {
		t.Errorf("Expected: %v but got: %v", expectedURL, url)
	}
}
示例#4
0
文件: main.go 项目: skiarn/mad-mock
func main() {
	errorlog, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		fmt.Printf("error opening file: %v", err)
		os.Exit(1)
	}
	defer errorlog.Close()
	logger = log.New(errorlog, "applog: ", log.Lshortfile|log.LstdFlags)

	settings, err := setting.Create()
	if err != nil {
		logger.Fatal(err)
	}
	err = settings.CreateDir()
	if err != nil {
		logger.Fatal(err)
	}
	mux := http.NewServeMux()
	pagehandler := handler.NewPageHandler(settings.DataDirPath, settings.TargetURL)
	curdhandler := handler.NewMockCURDHandler(settings.DataDirPath)
	viewDataHandler := handler.NewViewDataHandler(settings.DataDirPath)

	wsHandler := ws.NewHandler(logger)
	mockhandler := handler.NewMockhandler(settings.TargetURL, settings.DataDirPath, *wsHandler)
	mux.Handle("/mock/wsmockinfo", websocket.Handler(wsHandler.WSMockInfoServer))
	go wsHandler.Run()

	mux.Handle("/mock", &pagehandler)
	mux.Handle("/mock/api/mock/", &curdhandler)
	mux.Handle("/mock/api/mock/data/", &viewDataHandler)

	mux.HandleFunc("/mock/edit/", pagehandler.EditHandler)
	mux.HandleFunc("/mock/new/", pagehandler.New)
	mux.Handle("/", &mockhandler)
	logger.Printf("Server to listen on a port: %v \n", settings.Port)
	logger.Fatal(http.ListenAndServe(":"+strconv.Itoa(settings.Port), mux))
}