| 25 | } |
| 26 | |
| 27 | func TestDispatchHello(t *testing.T) { |
| 28 | s := &Session{ |
| 29 | send: make(chan any, 10), |
| 30 | uid: types.Uid(1), |
| 31 | authLvl: auth.LevelAuth, |
| 32 | } |
| 33 | wg := sync.WaitGroup{} |
| 34 | r := responses{} |
| 35 | wg.Add(1) |
| 36 | go s.testWriteLoop(&r, &wg) |
| 37 | msg := &ClientComMessage{ |
| 38 | Hi: &MsgClientHi{ |
| 39 | Id: "123", |
| 40 | Version: "1", |
| 41 | UserAgent: "test-ua", |
| 42 | Lang: "en-GB", |
| 43 | }, |
| 44 | } |
| 45 | s.dispatch(msg) |
| 46 | close(s.send) |
| 47 | wg.Wait() |
| 48 | if len(r.messages) != 1 { |
| 49 | t.Errorf("responses: expected 1, received %d.", len(r.messages)) |
| 50 | } |
| 51 | resp := r.messages[0].(*ServerComMessage) |
| 52 | if resp == nil { |
| 53 | t.Fatal("Response must be ServerComMessage") |
| 54 | } |
| 55 | if resp.Ctrl != nil { |
| 56 | if resp.Ctrl.Code != 201 { |
| 57 | t.Errorf("Response code: expected 201, got %d", resp.Ctrl.Code) |
| 58 | } |
| 59 | if resp.Ctrl.Params == nil { |
| 60 | t.Error("Response is expected to contain params dict.") |
| 61 | } |
| 62 | } else { |
| 63 | t.Error("Response must contain a ctrl message.") |
| 64 | } |
| 65 | |
| 66 | if s.lang != "en-GB" { |
| 67 | t.Errorf("Session language expected to be 'en-GB' vs '%s'", s.lang) |
| 68 | } |
| 69 | if s.userAgent != "test-ua" { |
| 70 | t.Errorf("Session UA expected to be 'test-ua' vs '%s'", s.userAgent) |
| 71 | } |
| 72 | if s.countryCode != "GB" { |
| 73 | t.Errorf("Country code expected to be 'GB' vs '%s'", s.countryCode) |
| 74 | } |
| 75 | if s.ver == 0 { |
| 76 | t.Errorf("s.ver expected 0 vs found %d", s.ver) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func verifyResponseCodes(r *responses, codes []int, t *testing.T) { |
| 81 | if len(r.messages) != len(codes) { |