Removed godep dependency.
Golang community are discuting a vendor aproach to go dependencies. This change will come as experimental in go 1.5. For now I am removing the godep dependency manager and using a vendor aproach. I still have to use the full path when importing dependencies, but when go1.5 released I just have to rename the imports and use a -vendor prefix when building the project.
This commit is contained in:
Generated
-26
@@ -1,26 +0,0 @@
|
||||
{
|
||||
"ImportPath": "github.com/dimiro1/ipe",
|
||||
"GoVersion": "go1.4.1",
|
||||
"Deps": [
|
||||
{
|
||||
"ImportPath": "github.com/goji/httpauth",
|
||||
"Rev": "74459b2b27aa334fc9427c1604ee304c6da57661"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/golang/glog",
|
||||
"Rev": "44145f04b68cf362d9c4df2182967c2275eaefed"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/gorilla/context",
|
||||
"Rev": "215affda49addc4c8ef7e2534915df2c8c35c6cd"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/gorilla/mux",
|
||||
"Rev": "e444e69cbd2e2e3e0749a2f3c717cec491552bbf"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/gorilla/websocket",
|
||||
"Rev": "9007e29a7c93fc3dcebbdbf742ce0efaf19e6510"
|
||||
}
|
||||
]
|
||||
}
|
||||
Generated
-5
@@ -1,5 +0,0 @@
|
||||
This directory tree is generated automatically by godep.
|
||||
|
||||
Please do not edit.
|
||||
|
||||
See https://github.com/tools/godep for more information.
|
||||
Generated
-2
@@ -1,2 +0,0 @@
|
||||
/pkg
|
||||
/bin
|
||||
+1
-1
@@ -10,7 +10,7 @@ import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
log "github.com/golang/glog"
|
||||
log "github.com/dimiro1/ipe/vendor/github.com/golang/glog"
|
||||
)
|
||||
|
||||
// An App
|
||||
|
||||
+2
-2
@@ -10,8 +10,8 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
log "github.com/golang/glog"
|
||||
"github.com/gorilla/mux"
|
||||
log "github.com/dimiro1/ipe/vendor/github.com/golang/glog"
|
||||
"github.com/dimiro1/ipe/vendor/github.com/gorilla/mux"
|
||||
|
||||
"github.com/dimiro1/ipe/utils"
|
||||
)
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
log "github.com/golang/glog"
|
||||
log "github.com/dimiro1/ipe/vendor/github.com/golang/glog"
|
||||
)
|
||||
|
||||
// A Channel
|
||||
|
||||
+2
-2
@@ -7,8 +7,8 @@ package ipe
|
||||
import (
|
||||
"time"
|
||||
|
||||
log "github.com/golang/glog"
|
||||
"github.com/gorilla/websocket"
|
||||
log "github.com/dimiro1/ipe/vendor/github.com/golang/glog"
|
||||
"github.com/dimiro1/ipe/vendor/github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
// An User Connection
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ package ipe
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
log "github.com/golang/glog"
|
||||
log "github.com/dimiro1/ipe/vendor/github.com/golang/glog"
|
||||
)
|
||||
|
||||
// {
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/dimiro1/ipe/vendor/github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// Check if the application is disabled
|
||||
|
||||
+2
-2
@@ -10,8 +10,8 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
log "github.com/golang/glog"
|
||||
"github.com/gorilla/mux"
|
||||
log "github.com/dimiro1/ipe/vendor/github.com/golang/glog"
|
||||
"github.com/dimiro1/ipe/vendor/github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// An event consists of a name and data (typically JSON) which may be sent to all subscribers to a particular channel or channels.
|
||||
|
||||
+2
-2
@@ -8,8 +8,8 @@ import (
|
||||
_ "expvar"
|
||||
"net/http"
|
||||
|
||||
"github.com/goji/httpauth"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/dimiro1/ipe/vendor/github.com/goji/httpauth"
|
||||
"github.com/dimiro1/ipe/vendor/github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// NewRouter is a function that returns a new configured Router
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/dimiro1/ipe/utils"
|
||||
log "github.com/golang/glog"
|
||||
log "github.com/dimiro1/ipe/vendor/github.com/golang/glog"
|
||||
)
|
||||
|
||||
// A WebHook is sent as a HTTP POST request to the url which you specify.
|
||||
|
||||
+3
-3
@@ -12,9 +12,9 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
log "github.com/golang/glog"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/gorilla/websocket"
|
||||
log "github.com/dimiro1/ipe/vendor/github.com/golang/glog"
|
||||
"github.com/dimiro1/ipe/vendor/github.com/gorilla/mux"
|
||||
"github.com/dimiro1/ipe/vendor/github.com/gorilla/websocket"
|
||||
|
||||
"github.com/dimiro1/ipe/utils"
|
||||
)
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/dimiro1/ipe/ipe"
|
||||
log "github.com/golang/glog"
|
||||
log "github.com/dimiro1/ipe/vendor/github.com/golang/glog"
|
||||
)
|
||||
|
||||
// Main function, initialze the system
|
||||
|
||||
Generated
Vendored
Generated
Vendored
Generated
Vendored
Generated
Vendored
Generated
Vendored
Generated
Vendored
Generated
Vendored
Generated
Vendored
Generated
Vendored
Generated
Vendored
Generated
Vendored
Generated
Vendored
Generated
Vendored
Generated
Vendored
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
+5
-5
@@ -89,7 +89,7 @@ There are several other matchers that can be added. To match path prefixes:
|
||||
|
||||
r.MatcherFunc(func(r *http.Request, rm *RouteMatch) bool {
|
||||
return r.ProtoMajor == 0
|
||||
})
|
||||
})
|
||||
|
||||
...and finally, it is possible to combine several matchers in a single route:
|
||||
|
||||
@@ -164,8 +164,8 @@ This also works for host variables:
|
||||
|
||||
// url.String() will be "http://news.domain.com/articles/technology/42"
|
||||
url, err := r.Get("article").URL("subdomain", "news",
|
||||
"category", "technology",
|
||||
"id", "42")
|
||||
"category", "technology",
|
||||
"id", "42")
|
||||
|
||||
All variables defined in the route are required, and their values must
|
||||
conform to the corresponding patterns. These requirements guarantee that a
|
||||
@@ -193,7 +193,7 @@ as well:
|
||||
|
||||
// "http://news.domain.com/articles/technology/42"
|
||||
url, err := r.Get("article").URL("subdomain", "news",
|
||||
"category", "technology",
|
||||
"id", "42")
|
||||
"category", "technology",
|
||||
"id", "42")
|
||||
*/
|
||||
package mux
|
||||
Generated
Vendored
Regular → Executable
+13
@@ -152,6 +152,13 @@ func (r *Router) getRegexpGroup() *routeRegexpGroup {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Router) buildVars(m map[string]string) map[string]string {
|
||||
if r.parent != nil {
|
||||
m = r.parent.buildVars(m)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Route factories
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -224,6 +231,12 @@ func (r *Router) Schemes(schemes ...string) *Route {
|
||||
return r.NewRoute().Schemes(schemes...)
|
||||
}
|
||||
|
||||
// BuildVars registers a new route with a custom function for modifying
|
||||
// route variables before building a URL.
|
||||
func (r *Router) BuildVarsFunc(f BuildVarsFunc) *Route {
|
||||
return r.NewRoute().BuildVarsFunc(f)
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Context
|
||||
// ----------------------------------------------------------------------------
|
||||
Godeps/_workspace/src/github.com/gorilla/mux/mux_test.go → vendor/github.com/gorilla/mux/mux_test.go
Generated
Vendored
Regular → Executable
+69
@@ -135,6 +135,42 @@ func TestHost(t *testing.T) {
|
||||
path: "",
|
||||
shouldMatch: false,
|
||||
},
|
||||
{
|
||||
title: "Path route with single pattern with pipe, match",
|
||||
route: new(Route).Path("/{category:a|b/c}"),
|
||||
request: newRequest("GET", "http://localhost/a"),
|
||||
vars: map[string]string{"category": "a"},
|
||||
host: "",
|
||||
path: "/a",
|
||||
shouldMatch: true,
|
||||
},
|
||||
{
|
||||
title: "Path route with single pattern with pipe, match",
|
||||
route: new(Route).Path("/{category:a|b/c}"),
|
||||
request: newRequest("GET", "http://localhost/b/c"),
|
||||
vars: map[string]string{"category": "b/c"},
|
||||
host: "",
|
||||
path: "/b/c",
|
||||
shouldMatch: true,
|
||||
},
|
||||
{
|
||||
title: "Path route with multiple patterns with pipe, match",
|
||||
route: new(Route).Path("/{category:a|b/c}/{product}/{id:[0-9]+}"),
|
||||
request: newRequest("GET", "http://localhost/a/product_name/1"),
|
||||
vars: map[string]string{"category": "a", "product": "product_name", "id": "1"},
|
||||
host: "",
|
||||
path: "/a/product_name/1",
|
||||
shouldMatch: true,
|
||||
},
|
||||
{
|
||||
title: "Path route with multiple patterns with pipe, match",
|
||||
route: new(Route).Path("/{category:a|b/c}/{product}/{id:[0-9]+}"),
|
||||
request: newRequest("GET", "http://localhost/b/c/product_name/1"),
|
||||
vars: map[string]string{"category": "b/c", "product": "product_name", "id": "1"},
|
||||
host: "",
|
||||
path: "/b/c/product_name/1",
|
||||
shouldMatch: true,
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
testRoute(t, test)
|
||||
@@ -593,6 +629,39 @@ func TestMatcherFunc(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildVarsFunc(t *testing.T) {
|
||||
tests := []routeTest{
|
||||
{
|
||||
title: "BuildVarsFunc set on route",
|
||||
route: new(Route).Path(`/111/{v1:\d}{v2:.*}`).BuildVarsFunc(func(vars map[string]string) map[string]string {
|
||||
vars["v1"] = "3"
|
||||
vars["v2"] = "a"
|
||||
return vars
|
||||
}),
|
||||
request: newRequest("GET", "http://localhost/111/2"),
|
||||
path: "/111/3a",
|
||||
shouldMatch: true,
|
||||
},
|
||||
{
|
||||
title: "BuildVarsFunc set on route and parent route",
|
||||
route: new(Route).PathPrefix(`/{v1:\d}`).BuildVarsFunc(func(vars map[string]string) map[string]string {
|
||||
vars["v1"] = "2"
|
||||
return vars
|
||||
}).Subrouter().Path(`/{v2:\w}`).BuildVarsFunc(func(vars map[string]string) map[string]string {
|
||||
vars["v2"] = "b"
|
||||
return vars
|
||||
}),
|
||||
request: newRequest("GET", "http://localhost/1/a"),
|
||||
path: "/2/b",
|
||||
shouldMatch: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
testRoute(t, test)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubRouter(t *testing.T) {
|
||||
subrouter1 := new(Route).Host("{v1:[a-z]+}.google.com").Subrouter()
|
||||
subrouter2 := new(Route).PathPrefix("/foo/{v1}").Subrouter()
|
||||
Godeps/_workspace/src/github.com/gorilla/mux/old_test.go → vendor/github.com/gorilla/mux/old_test.go
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
+1
-5
@@ -150,11 +150,7 @@ func (r *routeRegexp) Match(req *http.Request, match *RouteMatch) bool {
|
||||
}
|
||||
|
||||
// url builds a URL part using the given values.
|
||||
func (r *routeRegexp) url(pairs ...string) (string, error) {
|
||||
values, err := mapFromPairs(pairs...)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
func (r *routeRegexp) url(values map[string]string) (string, error) {
|
||||
urlValues := make([]interface{}, len(r.varsN))
|
||||
for k, v := range r.varsN {
|
||||
value, ok := values[v]
|
||||
Generated
Vendored
Regular → Executable
+55
-8
@@ -31,6 +31,8 @@ type Route struct {
|
||||
name string
|
||||
// Error resulted from building a route.
|
||||
err error
|
||||
|
||||
buildVarsFunc BuildVarsFunc
|
||||
}
|
||||
|
||||
// Match matches the route against the request.
|
||||
@@ -212,7 +214,7 @@ func (r *Route) Headers(pairs ...string) *Route {
|
||||
|
||||
// Host adds a matcher for the URL host.
|
||||
// It accepts a template with zero or more URL variables enclosed by {}.
|
||||
// Variables can define an optional regexp pattern to me matched:
|
||||
// Variables can define an optional regexp pattern to be matched:
|
||||
//
|
||||
// - {name} matches anything until the next dot.
|
||||
//
|
||||
@@ -270,7 +272,7 @@ func (r *Route) Methods(methods ...string) *Route {
|
||||
// Path adds a matcher for the URL path.
|
||||
// It accepts a template with zero or more URL variables enclosed by {}. The
|
||||
// template must start with a "/".
|
||||
// Variables can define an optional regexp pattern to me matched:
|
||||
// Variables can define an optional regexp pattern to be matched:
|
||||
//
|
||||
// - {name} matches anything until the next slash.
|
||||
//
|
||||
@@ -321,7 +323,7 @@ func (r *Route) PathPrefix(tpl string) *Route {
|
||||
//
|
||||
// It the value is an empty string, it will match any value if the key is set.
|
||||
//
|
||||
// Variables can define an optional regexp pattern to me matched:
|
||||
// Variables can define an optional regexp pattern to be matched:
|
||||
//
|
||||
// - {name} matches anything until the next slash.
|
||||
//
|
||||
@@ -360,6 +362,19 @@ func (r *Route) Schemes(schemes ...string) *Route {
|
||||
return r.addMatcher(schemeMatcher(schemes))
|
||||
}
|
||||
|
||||
// BuildVarsFunc --------------------------------------------------------------
|
||||
|
||||
// BuildVarsFunc is the function signature used by custom build variable
|
||||
// functions (which can modify route variables before a route's URL is built).
|
||||
type BuildVarsFunc func(map[string]string) map[string]string
|
||||
|
||||
// BuildVarsFunc adds a custom function to be used to modify build variables
|
||||
// before a route's URL is built.
|
||||
func (r *Route) BuildVarsFunc(f BuildVarsFunc) *Route {
|
||||
r.buildVarsFunc = f
|
||||
return r
|
||||
}
|
||||
|
||||
// Subrouter ------------------------------------------------------------------
|
||||
|
||||
// Subrouter creates a subrouter for the route.
|
||||
@@ -422,17 +437,20 @@ func (r *Route) URL(pairs ...string) (*url.URL, error) {
|
||||
if r.regexp == nil {
|
||||
return nil, errors.New("mux: route doesn't have a host or path")
|
||||
}
|
||||
values, err := r.prepareVars(pairs...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var scheme, host, path string
|
||||
var err error
|
||||
if r.regexp.host != nil {
|
||||
// Set a default scheme.
|
||||
scheme = "http"
|
||||
if host, err = r.regexp.host.url(pairs...); err != nil {
|
||||
if host, err = r.regexp.host.url(values); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if r.regexp.path != nil {
|
||||
if path, err = r.regexp.path.url(pairs...); err != nil {
|
||||
if path, err = r.regexp.path.url(values); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -453,7 +471,11 @@ func (r *Route) URLHost(pairs ...string) (*url.URL, error) {
|
||||
if r.regexp == nil || r.regexp.host == nil {
|
||||
return nil, errors.New("mux: route doesn't have a host")
|
||||
}
|
||||
host, err := r.regexp.host.url(pairs...)
|
||||
values, err := r.prepareVars(pairs...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
host, err := r.regexp.host.url(values)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -473,7 +495,11 @@ func (r *Route) URLPath(pairs ...string) (*url.URL, error) {
|
||||
if r.regexp == nil || r.regexp.path == nil {
|
||||
return nil, errors.New("mux: route doesn't have a path")
|
||||
}
|
||||
path, err := r.regexp.path.url(pairs...)
|
||||
values, err := r.prepareVars(pairs...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
path, err := r.regexp.path.url(values)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -482,6 +508,26 @@ func (r *Route) URLPath(pairs ...string) (*url.URL, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// prepareVars converts the route variable pairs into a map. If the route has a
|
||||
// BuildVarsFunc, it is invoked.
|
||||
func (r *Route) prepareVars(pairs ...string) (map[string]string, error) {
|
||||
m, err := mapFromPairs(pairs...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.buildVars(m), nil
|
||||
}
|
||||
|
||||
func (r *Route) buildVars(m map[string]string) map[string]string {
|
||||
if r.parent != nil {
|
||||
m = r.parent.buildVars(m)
|
||||
}
|
||||
if r.buildVarsFunc != nil {
|
||||
m = r.buildVarsFunc(m)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// parentRoute
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -490,6 +536,7 @@ func (r *Route) URLPath(pairs ...string) (*url.URL, error) {
|
||||
type parentRoute interface {
|
||||
getNamedRoutes() map[string]*Route
|
||||
getRegexpGroup() *routeRegexpGroup
|
||||
buildVars(map[string]string) map[string]string
|
||||
}
|
||||
|
||||
// getNamedRoutes returns the map where named routes are registered.
|
||||
Generated
Vendored
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
+35
-1
@@ -5,8 +5,11 @@
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -127,6 +130,11 @@ func parseURL(s string) (*url.URL, error) {
|
||||
u.Opaque = s[i:]
|
||||
}
|
||||
|
||||
if strings.Contains(u.Host, "@") {
|
||||
// WebSocket URIs do not contain user information.
|
||||
return nil, errMalformedURL
|
||||
}
|
||||
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
@@ -155,7 +163,8 @@ var DefaultDialer *Dialer
|
||||
//
|
||||
// If the WebSocket handshake fails, ErrBadHandshake is returned along with a
|
||||
// non-nil *http.Response so that callers can handle redirects, authentication,
|
||||
// etc.
|
||||
// etcetera. The response body may not contain the entire response and does not
|
||||
// need to be closed by the application.
|
||||
func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
|
||||
u, err := parseURL(urlStr)
|
||||
if err != nil {
|
||||
@@ -224,8 +233,33 @@ func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Re
|
||||
requestHeader = h
|
||||
}
|
||||
|
||||
if len(requestHeader["Host"]) > 0 {
|
||||
// This can be used to supply a Host: header which is different from
|
||||
// the dial address.
|
||||
u.Host = requestHeader.Get("Host")
|
||||
|
||||
// Drop "Host" header
|
||||
h := http.Header{}
|
||||
for k, v := range requestHeader {
|
||||
if k == "Host" {
|
||||
continue
|
||||
}
|
||||
h[k] = v
|
||||
}
|
||||
requestHeader = h
|
||||
}
|
||||
|
||||
conn, resp, err := NewClient(netConn, u, requestHeader, d.ReadBufferSize, d.WriteBufferSize)
|
||||
|
||||
if err != nil {
|
||||
if err == ErrBadHandshake {
|
||||
// Before closing the network connection on return from this
|
||||
// function, slurp up some of the response to aid application
|
||||
// debugging.
|
||||
buf := make([]byte, 1024)
|
||||
n, _ := io.ReadFull(resp.Body, buf)
|
||||
resp.Body = ioutil.NopCloser(bytes.NewReader(buf[:n]))
|
||||
}
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
Generated
Vendored
Regular → Executable
+81
-7
@@ -8,11 +8,13 @@ import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -34,22 +36,22 @@ var cstDialer = Dialer{
|
||||
|
||||
type cstHandler struct{ *testing.T }
|
||||
|
||||
type Server struct {
|
||||
type cstServer struct {
|
||||
*httptest.Server
|
||||
URL string
|
||||
}
|
||||
|
||||
func newServer(t *testing.T) *Server {
|
||||
var s Server
|
||||
func newServer(t *testing.T) *cstServer {
|
||||
var s cstServer
|
||||
s.Server = httptest.NewServer(cstHandler{t})
|
||||
s.URL = "ws" + s.Server.URL[len("http"):]
|
||||
s.URL = makeWsProto(s.Server.URL)
|
||||
return &s
|
||||
}
|
||||
|
||||
func newTLSServer(t *testing.T) *Server {
|
||||
var s Server
|
||||
func newTLSServer(t *testing.T) *cstServer {
|
||||
var s cstServer
|
||||
s.Server = httptest.NewTLSServer(cstHandler{t})
|
||||
s.URL = "ws" + s.Server.URL[len("http"):]
|
||||
s.URL = makeWsProto(s.Server.URL)
|
||||
return &s
|
||||
}
|
||||
|
||||
@@ -97,6 +99,10 @@ func (t cstHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func makeWsProto(s string) string {
|
||||
return "ws" + strings.TrimPrefix(s, "http")
|
||||
}
|
||||
|
||||
func sendRecv(t *testing.T, ws *Conn) {
|
||||
const message = "Hello World!"
|
||||
if err := ws.SetWriteDeadline(time.Now().Add(time.Second)); err != nil {
|
||||
@@ -157,6 +163,7 @@ func TestDialTLS(t *testing.T) {
|
||||
}
|
||||
|
||||
func xTestDialTLSBadCert(t *testing.T) {
|
||||
// This test is deactivated because of noisy logging from the net/http package.
|
||||
s := newTLSServer(t)
|
||||
defer s.Close()
|
||||
|
||||
@@ -247,3 +254,70 @@ func TestHandshake(t *testing.T) {
|
||||
}
|
||||
sendRecv(t, ws)
|
||||
}
|
||||
|
||||
func TestRespOnBadHandshake(t *testing.T) {
|
||||
const expectedStatus = http.StatusGone
|
||||
const expectedBody = "This is the response body."
|
||||
|
||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(expectedStatus)
|
||||
io.WriteString(w, expectedBody)
|
||||
}))
|
||||
defer s.Close()
|
||||
|
||||
ws, resp, err := cstDialer.Dial(makeWsProto(s.URL), nil)
|
||||
if err == nil {
|
||||
ws.Close()
|
||||
t.Fatalf("Dial: nil")
|
||||
}
|
||||
|
||||
if resp == nil {
|
||||
t.Fatalf("resp=nil, err=%v", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != expectedStatus {
|
||||
t.Errorf("resp.StatusCode=%d, want %d", resp.StatusCode, expectedStatus)
|
||||
}
|
||||
|
||||
p, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFull(resp.Body) returned error %v", err)
|
||||
}
|
||||
|
||||
if string(p) != expectedBody {
|
||||
t.Errorf("resp.Body=%s, want %s", p, expectedBody)
|
||||
}
|
||||
}
|
||||
|
||||
// If the Host header is specified in `Dial()`, the server must receive it as
|
||||
// the `Host:` header.
|
||||
func TestHostHeader(t *testing.T) {
|
||||
s := newServer(t)
|
||||
defer s.Close()
|
||||
|
||||
specifiedHost := make(chan string, 1)
|
||||
origHandler := s.Server.Config.Handler
|
||||
|
||||
// Capture the request Host header.
|
||||
s.Server.Config.Handler = http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
specifiedHost <- r.Host
|
||||
origHandler.ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
ws, resp, err := cstDialer.Dial(s.URL, http.Header{"Host": {"testhost"}})
|
||||
if err != nil {
|
||||
t.Fatalf("Dial: %v", err)
|
||||
}
|
||||
defer ws.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusSwitchingProtocols {
|
||||
t.Fatalf("resp.StatusCode = %v, want http.StatusSwitchingProtocols", resp.StatusCode)
|
||||
}
|
||||
|
||||
if gotHost := <-specifiedHost; gotHost != "testhost" {
|
||||
t.Fatalf("gotHost = %q, want \"testhost\"", gotHost)
|
||||
}
|
||||
|
||||
sendRecv(t, ws)
|
||||
}
|
||||
Generated
Vendored
Regular → Executable
+1
@@ -20,6 +20,7 @@ var parseURLTests = []struct {
|
||||
{"wss://example.com/", &url.URL{Scheme: "wss", Host: "example.com", Opaque: "/"}},
|
||||
{"wss://example.com/a/b", &url.URL{Scheme: "wss", Host: "example.com", Opaque: "/a/b"}},
|
||||
{"ss://example.com/a/b", nil},
|
||||
{"ws://webmaster@example.com/", nil},
|
||||
}
|
||||
|
||||
func TestParseURL(t *testing.T) {
|
||||
Generated
Vendored
Regular → Executable
+1
-1
@@ -801,7 +801,7 @@ func (c *Conn) SetPingHandler(h func(string) error) {
|
||||
c.handlePing = h
|
||||
}
|
||||
|
||||
// SetPongHandler sets then handler for pong messages received from the peer.
|
||||
// SetPongHandler sets the handler for pong messages received from the peer.
|
||||
// The default pong handler does nothing.
|
||||
func (c *Conn) SetPongHandler(h func(string) error) {
|
||||
if h == nil {
|
||||
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
+1
-1
@@ -24,7 +24,7 @@
|
||||
// ... Use conn to send and receive messages.
|
||||
// }
|
||||
//
|
||||
// Call the connection WriteMessage and ReadMessages methods to send and
|
||||
// Call the connection's WriteMessage and ReadMessage methods to send and
|
||||
// receive messages as a slice of bytes. This snippet of code shows how to echo
|
||||
// messages using these methods:
|
||||
//
|
||||
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
+1
-1
@@ -147,7 +147,7 @@ func serveHome(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
io.WriteString(w, "<html><body>Echo Server</body></html")
|
||||
io.WriteString(w, "<html><body>Echo Server</body></html>")
|
||||
}
|
||||
|
||||
var addr = flag.String("addr", ":9000", "http service address")
|
||||
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
+9
-1
@@ -6,6 +6,7 @@ package websocket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
)
|
||||
|
||||
// WriteJSON is deprecated, use c.WriteJSON instead.
|
||||
@@ -45,5 +46,12 @@ func (c *Conn) ReadJSON(v interface{}) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.NewDecoder(r).Decode(v)
|
||||
err = json.NewDecoder(r).Decode(v)
|
||||
if err == io.EOF {
|
||||
// Decode returns io.EOF when the message is empty or all whitespace.
|
||||
// Convert to io.ErrUnexpectedEOF so that application can distinguish
|
||||
// between an error reading the JSON value and the connection closing.
|
||||
err = io.ErrUnexpectedEOF
|
||||
}
|
||||
return err
|
||||
}
|
||||
Generated
Vendored
Regular → Executable
+56
@@ -6,6 +6,8 @@ package websocket
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
@@ -36,6 +38,60 @@ func TestJSON(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPartialJsonRead(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
c := fakeNetConn{&buf, &buf}
|
||||
wc := newConn(c, true, 1024, 1024)
|
||||
rc := newConn(c, false, 1024, 1024)
|
||||
|
||||
var v struct {
|
||||
A int
|
||||
B string
|
||||
}
|
||||
v.A = 1
|
||||
v.B = "hello"
|
||||
|
||||
messageCount := 0
|
||||
|
||||
// Partial JSON values.
|
||||
|
||||
data, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := len(data) - 1; i >= 0; i-- {
|
||||
if err := wc.WriteMessage(TextMessage, data[:i]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
messageCount++
|
||||
}
|
||||
|
||||
// Whitespace.
|
||||
|
||||
if err := wc.WriteMessage(TextMessage, []byte(" ")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
messageCount++
|
||||
|
||||
// Close.
|
||||
|
||||
if err := wc.WriteMessage(CloseMessage, FormatCloseMessage(CloseNormalClosure, "")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for i := 0; i < messageCount; i++ {
|
||||
err := rc.ReadJSON(&v)
|
||||
if err != io.ErrUnexpectedEOF {
|
||||
t.Error("read", i, err)
|
||||
}
|
||||
}
|
||||
|
||||
err = rc.ReadJSON(&v)
|
||||
if err != io.EOF {
|
||||
t.Error("final", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeprecatedJSON(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
c := fakeNetConn{&buf, &buf}
|
||||
Generated
Vendored
Regular → Executable
+2
-2
@@ -98,11 +98,11 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
|
||||
}
|
||||
|
||||
if !tokenListContainsValue(r.Header, "Connection", "upgrade") {
|
||||
return u.returnError(w, r, http.StatusBadRequest, "websocket: connection header != upgrade")
|
||||
return u.returnError(w, r, http.StatusBadRequest, "websocket: could not find connection header with token 'upgrade'")
|
||||
}
|
||||
|
||||
if !tokenListContainsValue(r.Header, "Upgrade", "websocket") {
|
||||
return u.returnError(w, r, http.StatusBadRequest, "websocket: upgrade != websocket")
|
||||
return u.returnError(w, r, http.StatusBadRequest, "websocket: could not find upgrade header with token 'websocket'")
|
||||
}
|
||||
|
||||
checkOrigin := u.CheckOrigin
|
||||
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
Generated
Vendored
Regular → Executable
Reference in New Issue
Block a user