Click here to Skip to main content
15,902,276 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
}


What I have tried:

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.
Posted
Comments
[no name] 8-Apr-19 10:59am    
Not too many people using "GO" around here. Far as I can tell, you are trying to make a connection without receiving or sending anything; so, it's not going to be much of a "test".
Member 14050287 8-Apr-19 11:05am    
Thanks for your reply. I have them but currently, I just want to test this function

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900