Skip to content

Commit

Permalink
add example test for socket client and remove useless command
Browse files Browse the repository at this point in the history
  • Loading branch information
haya14busa committed Sep 1, 2016
1 parent ffafc17 commit 366af8f
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions cmd/goplaysock/client.go → socket/client_test.go
@@ -1,23 +1,29 @@
package main
package socket

import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"

goplay "github.com/haya14busa/goplay/socket"

"golang.org/x/net/websocket"
"golang.org/x/tools/playground/socket"
)

const origin = "http://127.0.0.1/"

const code = `
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界!")
}
`

func ExampleClient() {
// Serve websocket playground server
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
Expand All @@ -30,7 +36,11 @@ func main() {
defer l.Close()
mu := http.NewServeMux()

mu.Handle("/", socket.NewHandler(mustParseURL(fmt.Sprintf("http://%s", l.Addr()))))
u, err := url.Parse(fmt.Sprintf("http://%s", l.Addr()))
if err != nil {
log.Fatal(err)
}
mu.Handle("/", socket.NewHandler(u))
s := http.Server{Handler: mu}
go s.Serve(l)

Expand All @@ -44,15 +54,8 @@ func main() {
if err != nil {
log.Fatal(err)
}
cli := goplay.Client{Conn: ws}
code, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
cli.Run(string(code))
}

func mustParseURL(u string) *url.URL {
r, _ := url.Parse(u)
return r
cli := &Client{Conn: ws}
cli.Run(code)
// Output:
// Hello, 世界!
}

0 comments on commit 366af8f

Please sign in to comment.