|
| 1 | +package websocket_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/hex" |
| 6 | + "fmt" |
| 7 | + "math/rand" |
| 8 | + "reflect" |
| 9 | + |
| 10 | + "github.com/google/go-cmp/cmp" |
| 11 | + |
| 12 | + "nhooyr.io/websocket" |
| 13 | + "nhooyr.io/websocket/wsjson" |
| 14 | +) |
| 15 | + |
| 16 | +// https://github.com/google/go-cmp/issues/40#issuecomment-328615283 |
| 17 | +func cmpDiff(exp, act interface{}) string { |
| 18 | + return cmp.Diff(exp, act, deepAllowUnexported(exp, act)) |
| 19 | +} |
| 20 | + |
| 21 | +func deepAllowUnexported(vs ...interface{}) cmp.Option { |
| 22 | + m := make(map[reflect.Type]struct{}) |
| 23 | + for _, v := range vs { |
| 24 | + structTypes(reflect.ValueOf(v), m) |
| 25 | + } |
| 26 | + var typs []interface{} |
| 27 | + for t := range m { |
| 28 | + typs = append(typs, reflect.New(t).Elem().Interface()) |
| 29 | + } |
| 30 | + return cmp.AllowUnexported(typs...) |
| 31 | +} |
| 32 | + |
| 33 | +func structTypes(v reflect.Value, m map[reflect.Type]struct{}) { |
| 34 | + if !v.IsValid() { |
| 35 | + return |
| 36 | + } |
| 37 | + switch v.Kind() { |
| 38 | + case reflect.Ptr: |
| 39 | + if !v.IsNil() { |
| 40 | + structTypes(v.Elem(), m) |
| 41 | + } |
| 42 | + case reflect.Interface: |
| 43 | + if !v.IsNil() { |
| 44 | + structTypes(v.Elem(), m) |
| 45 | + } |
| 46 | + case reflect.Slice, reflect.Array: |
| 47 | + for i := 0; i < v.Len(); i++ { |
| 48 | + structTypes(v.Index(i), m) |
| 49 | + } |
| 50 | + case reflect.Map: |
| 51 | + for _, k := range v.MapKeys() { |
| 52 | + structTypes(v.MapIndex(k), m) |
| 53 | + } |
| 54 | + case reflect.Struct: |
| 55 | + m[v.Type()] = struct{}{} |
| 56 | + for i := 0; i < v.NumField(); i++ { |
| 57 | + structTypes(v.Field(i), m) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func assertEqualf(exp, act interface{}, f string, v ...interface{}) error { |
| 63 | + if diff := cmpDiff(exp, act); diff != "" { |
| 64 | + return fmt.Errorf(f+": %v", append(v, diff)...) |
| 65 | + } |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +func assertJSONEcho(ctx context.Context, c *websocket.Conn, n int) error { |
| 70 | + exp := randString(n) |
| 71 | + err := wsjson.Write(ctx, c, exp) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + var act interface{} |
| 77 | + err = wsjson.Read(ctx, c, &act) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + return assertEqualf(exp, act, "unexpected JSON") |
| 83 | +} |
| 84 | + |
| 85 | +func assertJSONRead(ctx context.Context, c *websocket.Conn, exp interface{}) error { |
| 86 | + var act interface{} |
| 87 | + err := wsjson.Read(ctx, c, &act) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + return assertEqualf(exp, act, "unexpected JSON") |
| 93 | +} |
| 94 | + |
| 95 | +func randBytes(n int) []byte { |
| 96 | + b := make([]byte, n) |
| 97 | + rand.Read(b) |
| 98 | + return b |
| 99 | +} |
| 100 | + |
| 101 | +func randString(n int) string { |
| 102 | + return hex.EncodeToString(randBytes(n))[:n] |
| 103 | +} |
| 104 | + |
| 105 | +func assertEcho(ctx context.Context, c *websocket.Conn, typ websocket.MessageType, n int) error { |
| 106 | + p := randBytes(n) |
| 107 | + err := c.Write(ctx, typ, p) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + typ2, p2, err := c.Read(ctx) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + err = assertEqualf(typ, typ2, "unexpected data type") |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + return assertEqualf(p, p2, "unexpected payload") |
| 120 | +} |
| 121 | + |
| 122 | +func assertSubprotocol(c *websocket.Conn, exp string) error { |
| 123 | + return assertEqualf(exp, c.Subprotocol(), "unexpected subprotocol") |
| 124 | +} |
0 commit comments