コード例 #1
0
func init() {
	rackup := rack.New()
	rackup.Add(Form)
	rackup.Add(HttpWare)

	conn := httper.HttpConnection(":4007")
	go conn.Go(rackup)
}
コード例 #2
0
func init() {
	rackup := rack.New()
	rackup.Add(parser.Form)
	rackup.Add(Override)
	rackup.Add(HttpWare)

	conn := httper.HttpConnection(":4005")
	go conn.Go(rackup)
}
コード例 #3
0
func Example_NoLogger() {
	rackup := rack.New()
	rackup.Add(HelloWorldWare)

	conn := httper.HttpConnection(":4004")
	go conn.Go(rackup)
	http.Get("http://localhost:4004")
	//output:
}
コード例 #4
0
func Example_Basic() {
	rackup := rack.New()
	rackup.Add(New("/static/", "./test_files"))

	conn := httper.HttpConnection(":4013")
	go conn.Go(rackup)

	GetFrom("http://localhost:4013/static/test.txt")
	//output: Hello World!
}
コード例 #5
0
func Example_Skipped() {
	rackup := rack.New()
	rackup.Add(HttpWare)

	conn := httper.HttpConnection(":3001")
	go conn.Go(rackup)

	PostTo("http://localhost:3001", url.Values{"Name": {"Jim"}})
	//output: Welcome, Jim
}
コード例 #6
0
func Example_Basic() {
	rackup := rack.New()
	rackup.Add(New(os.Stdout, "Log Test - ", 0))
	rackup.Add(HelloWorldWare)

	conn := httper.HttpConnection(":4003")
	go conn.Go(rackup)
	http.Get("http://localhost:4003")
	//output: Log Test - Hello World!
}
コード例 #7
0
func Example_Basic() {
	rackup := rack.New()
	rackup.Add(logger.New(os.Stdout, "", 0))
	rackup.Add(Logger)

	conn := httper.HttpConnection(":4009")
	go conn.Go(rackup)
	http.Get("http://localhost:4009/location")
	//output: GET /location
}
コード例 #8
0
func init() {
	cept := New()
	cept.Intercept("/helloworld", HelloWorldWare)

	rackup := rack.New()
	rackup.Add(cept)
	rackup.Add(RootWare)

	conn := httper.HttpConnection(":4002")
	go conn.Go(rackup)
}
コード例 #9
0
func Example_Multipart() {
	rackup := rack.New()
	rackup.Add(Multipart{256})
	rackup.Add(FileWare)

	conn := httper.HttpConnection(":3002")
	go conn.Go(rackup)

	SendFileTo("http://localhost:3002", "./test_files/helloworld.txt")
	//output: Hello World
}
コード例 #10
0
func Example_NoError() {
	rackup := rack.New()
	rackup.Add(ErrorHandler)
	rackup.Add(rack.Func(func(vars map[string]interface{}, next func()) {
		httper.V(vars).SetMessageString("Just Fine!")
	}))

	conn := httper.HttpConnection(":3002")
	go conn.Go(rackup)

	GetFrom("http://localhost:3002/")
	//output: Just Fine!
}
コード例 #11
0
func Example_Specific() {
	rackup := rack.New()
	rackup.Add(templater.GetTemplates("test_templates"))
	rackup.Add(encapsulator.AddLayout)
	rackup.Add(SetErrorLayout)

	conn := httper.HttpConnection(":3001")
	go conn.Go(rackup)

	GetFrom("http://localhost:3001/")

	//output: Not Found
}
コード例 #12
0
func init() {
	//set up oauth host

	hoster := &FakeProvider{
		YourURL:     "http://localhost:4006",
		YourLanding: "/callback",
		MyURL:       "http://localhost:3001",
	}

	hostconn := httper.HttpConnection(":3001")
	go hostconn.Go(hoster.Middleware())

	//set up our site
	cept := New(hoster, TokenHandlerFunc)

	rackup := rack.New()
	rackup.Add(sessioner.Middleware)
	rackup.Add(cept)

	conn := httper.HttpConnection(":4006")
	go conn.Go(rackup)
}
コード例 #13
0
func Example_SpecificOverride() {
	rackup := rack.New()
	rackup.Add(templater.GetTemplates("test_templates"))
	rackup.Add(encapsulator.AddLayout)
	rackup.Add(SetErrorLayout)
	rackup.Add(ErrorWare2)

	conn := httper.HttpConnection(":3002")
	go conn.Go(rackup)

	GetFrom("http://localhost:3002/")

	//output: Not Implemented!
}
コード例 #14
0
func Example_General() {
	rackup := rack.New()
	rackup.Add(templater.GetTemplates("test_templates"))
	rackup.Add(encapsulator.AddLayout)
	rackup.Add(SetErrorLayout)
	rackup.Add(ErrorWare)

	conn := httper.HttpConnection(":4014")
	go conn.Go(rackup)

	GetFrom("http://localhost:4014/")

	//output: Error - 500
}
コード例 #15
0
func Example_Basic() {
	rackup := rack.New()
	rackup.Add(V{"world": "World!"})
	rackup.Add(rack.Func(func(vars map[string]interface{}, next func()) {
		world := vars["world"].(string)
		(httper.V)(vars).SetMessageString("Hello " + world)
	}))

	conn := httper.HttpConnection(":4015")
	go conn.Go(rackup)

	GetFrom("http://localhost:4015/")
	//output: Hello World!
}
コード例 #16
0
func Example_Session() {
	rackup := rack.New()
	rackup.Add(Middleware)
	rackup.Add(HelloWorldWare)

	conn := httper.HttpConnection(":4012")
	go conn.Go(rackup)

	var cookies []*http.Cookie
	for i := 0; i < 4; i++ {
		cookies = GetWithCookies("http://localhost:4012", cookies)
	}
	//output: 1234
}
コード例 #17
0
func Example_BasicError() {
	rackup := rack.New()
	rackup.Add(ErrorHandler)
	rackup.Add(rack.Func(func(vars map[string]interface{}, next func()) {
		httper.V(vars).SetMessageString("Just Fine!")
		array := make([]byte, 0)
		array[1] = 0 //this action results in a runtime error; we are indexing past the range of the slice
	}))

	conn := httper.HttpConnection(":4001")
	go conn.Go(rackup)

	GetFrom("http://localhost:4001/")
	//output: runtime error: index out of range
}
コード例 #18
0
func Example_NoLayout() {
	rackup := rack.New()
	rackup.Add(templater.GetTemplates("./test_templates"))
	rackup.Add(AddLayout)
	rackup.Add(rack.Func(func(vars map[string]interface{}, next func()) {
		vars["Title"] = "Hello World"
		(httper.V)(vars).AppendMessageString("Hello World!")
	}))

	conn := httper.HttpConnection(":3001")
	go conn.Go(rackup)

	GetFrom("http://localhost:3001/")
	//output: Hello World!
}
コード例 #19
0
func Example_Basic() {
	rackup := rack.New()
	rackup.Add(templater.GetTemplates("./test_templates"))
	rackup.Add(AddLayout)
	rackup.Add(rack.Func(func(vars map[string]interface{}, next func()) {
		vars["Layout"] = "test"
		vars["Title"] = "Hello World"
		(httper.V)(vars).AppendMessageString("Hello World!")
	}))

	conn := httper.HttpConnection(":4000")
	go conn.Go(rackup)

	GetFrom("http://localhost:4000/")
	//output: <html><head><title>Hello World</title></head><body>Hello World!</body></html>
}
コード例 #20
0
func Example_Render() {
	rackup := rack.New()
	rackup.Add(logger.New(os.Stdout, "", 0))
	rackup.Add(templater.GetTemplates("./test_templates"))
	rackup.Add(rack.Func(func(vars map[string]interface{}, next func()) {
		vars["Object"] = "World"
		next()
	}))
	rackup.Add(Renderer{"test"})

	conn := httper.HttpConnection(":4010")
	go conn.Go(rackup)

	GetFrom("http://localhost:4010/")
	//output: Hello World
}
コード例 #21
0
func init() {
	jar, err := cookiejar.New(nil)
	if err != nil {
		panic("No Cookie Jar!")
	}
	client = &http.Client{Jar: jar}
	root := NewRoot(redirecter.Redirecter{"http://localhost:5001/coins"})
	coinRoute := NewResource(&Coins{})
	root.AddRoute(coinRoute)

	rackup := rack.New()
	rackup.Add(templater.GetTemplates("./test_templates"))
	rackup.Add(sessioner.Middleware)
	rackup.Add(root)

	conn := httper.HttpConnection(":5001")
	go conn.Go(rackup)
}
コード例 #22
0
func Example_Redirect() {
	rackup := rack.New()
	rackup.Add(rack.Func(func(vars map[string]interface{}, next func()) {
		h := (httper.V)(vars)
		path := h.GetRequest().URL.Path
		if path != "/" {
			h.SetMessageString(path)
		} else {
			next()
		}
	}))
	rackup.Add(Redirecter{"/redirected/"})

	conn := httper.HttpConnection(":4008")
	go conn.Go(rackup)

	GetFrom("http://localhost:4008/")
	//output: /redirected/
}
コード例 #23
0
func init() {
	var MemberRoute *Router = New()

	MemberRoute.Routing = SignalFunc(func(vars map[string]interface{}) bool {
		coinName := (V)(vars).CurrentSection()
		coinInfo, exists := coins[coinName]
		if !exists {
			return false
		}
		vars["Name"] = coinName
		vars["Info"] = coinInfo
		return true
	})

	MemberRoute.Action = rack.Func(func(vars map[string]interface{}, next func()) {
		name := vars["Name"].(string)
		info := vars["Info"].(string)
		(httper.V)(vars).SetMessageString(name + " - " + info + " - " + MemberRoute.Route(vars))
	})

	MemberRoute.Name = NamerFunc(func(vars map[string]interface{}) string {
		name := "(coin)"
		if coin, ok := vars["Name"].(string); ok {
			name = coin
		}
		return name
	})

	CollectionRoute := BasicRoute("coins", CoinCollectionWare)
	CollectionRoute.AddRoute(MemberRoute)

	Root := BasicRoute("", RootWare)
	Root.AddRoute(CollectionRoute)

	conn := httper.HttpConnection(":4011")
	go conn.Go(Root)
}
コード例 #24
0
func TestJSON(t *testing.T) {
	//setup test variables
	var isOpened, isClosed bool
	message := TestJSONMessage{}

	//create websocket handler
	ws := New()
	ws.OnOpen(rack.Func(func(vars map[string]interface{}, next func()) {
		isOpened = true
		next()
	}))
	ws.OnMessage(rack.Func(func(vars map[string]interface{}, next func()) {
		message = V(vars).GetMessage().(TestJSONMessage)
		fmt.Println(message)
		V(vars).SetResponse("Who's there?")
		next()
	}))
	ws.OnClose(rack.Func(func(vars map[string]interface{}, next func()) {
		isClosed = true
		next()
	}))

	defaultObj := TestJSONMessage{Type: "Blank", Data: map[string]interface{}{"test": "test"}}
	ws.ReceiveJSONObjects(defaultObj)

	//start a web server
	server := httper.HttpConnection(":4018")
	go server.Go(ws)

	//connect via websocket
	conn, err := websocket.Dial("ws://localhost:4018", "", "http://localhost")
	if err != nil {
		t.Fatal("Fatal - Can't open")
	}

	if !isOpened {
		t.Error("Should be open")
	}

	//send a message
	sendme := TestJSONMessage{Type: "text", Data: map[string]interface{}{"message": "Knock knock!"}}
	data, err := json.Marshal(sendme)
	if err != nil {
		t.Fatal("Fatal - Can't json")
	}

	if _, err := conn.Write(data); err != nil {
		t.Fatal("Fatal - Can't write")
	}

	//get a response
	response := make([]byte, 512)
	n, err := conn.Read(response)
	if err != nil {
		t.Fatal("Fatal - Can't read")
	}

	//response is in JSON, convert back to regular
	var response_object string
	json.Unmarshal(response[:n], &response_object)

	//check for message & response errors
	if message.Type != "text" {
		t.Error("Message Type Wrong - ", message.Type)
	}
	if message.Data["message"] != "Knock knock!" {
		t.Error("Message Wrong - ", message.Data["message"])
	}
	if response_object != "Who's there?" {
		t.Error("Response not passed through - ", response_object)
	}

	//and close
	if err := conn.Close(); err != nil {
		t.Fatal("Fatal - Can't close")
	}

	<-time.After(time.Second / 10)

	if !isClosed {
		t.Error("Should be closed")
	}
}
コード例 #25
0
func TestBinary(t *testing.T) {
	//setup test variables
	var isOpened, isClosed bool
	message := []byte{}

	//create websocket handler
	ws := New()
	ws.OnOpen(rack.Func(func(vars map[string]interface{}, next func()) {
		isOpened = true
		next()
	}))
	ws.OnMessage(rack.Func(func(vars map[string]interface{}, next func()) {
		message = V(vars).GetMessage().([]byte)
		V(vars).SetResponse([]byte("Who's there?"))
		next()
	}))
	ws.OnClose(rack.Func(func(vars map[string]interface{}, next func()) {
		isClosed = true
		next()
	}))
	ws.ReceiveBinaryMessages()

	//start a web server
	server := httper.HttpConnection(":4017")
	go server.Go(ws)

	//connect via websocket
	conn, err := websocket.Dial("ws://localhost:4017", "", "http://localhost")
	if err != nil {
		t.Fatal("Fatal - Can't open")
	}

	if !isOpened {
		t.Error("Should be open")
	}

	//send a message
	if _, err := conn.Write([]byte("Knock knock!")); err != nil {
		t.Fatal("Fatal - Can't write")
	}

	//get a response
	response := make([]byte, 512)
	n, err := conn.Read(response)
	if err != nil {
		t.Fatal("Fatal - Can't read")
	}

	if !bytes.Equal(message, []byte("Knock knock!")) {
		t.Error("Message not passed through")
	}
	if !bytes.Equal(response[:n], []byte("Who's there?")) {
		t.Error("Response not passed through")
	}

	//and close
	if err := conn.Close(); err != nil {
		t.Fatal("Fatal - Can't close")
	}

	<-time.After(time.Second / 10)

	if !isClosed {
		t.Error("Should be closed")
	}
}