type Websocket struct { upgrader *websocket.Upgrader CmdProxy proxy.CmdProxy connections map[uint64]map[*websocket.Conn]bool init sync.Once lock sync.Mutex } func (w *Websocket) Upgrade(thingId uint64, wr http.ResponseWriter, r *http.Request) error { w.init.Do(func() { w.connections = make(map[uint64]map[*websocket.Conn]bool) w.upgrader = &websocket.Upgrader{ CheckOrigin: func(r *http.Request) bool { return true }, Error: func(w http.ResponseWriter, r *http.Request, status int, reason error) {}, } }) conn, err := w.upgrader.Upgrade(wr, r, map[string][]string{ "Sec-Websocket-Protocol": {r.Context().Value(services.TokenKey).(string)}, }) if err != nil { return err } w.lock.Lock() connections, exists := w.connections[thingId] if !exists { connections = make(map[*websocket.Conn]bool) } connections[conn] = true w.connections[thingId] = connections w.lock.Unlock() // start new handling w.handle(thingId, conn) return nil }
Hello everyone, This is my WebSocket Upgrade function. I want to test this golang code. since I am new to the testing world i am raising this issue here especially about the response write which is an interface and i don't know how to mock it.
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)