user_json_test.go 944 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // +build json
  2. package protocol_test
  3. import (
  4. "encoding/json"
  5. "testing"
  6. . "github.com/v2ray/v2ray-core/common/protocol"
  7. v2testing "github.com/v2ray/v2ray-core/testing"
  8. "github.com/v2ray/v2ray-core/testing/assert"
  9. )
  10. func TestUserParsing(t *testing.T) {
  11. v2testing.Current(t)
  12. user := new(User)
  13. err := json.Unmarshal([]byte(`{
  14. "id": "96edb838-6d68-42ef-a933-25f7ac3a9d09",
  15. "email": "love@v2ray.com",
  16. "level": 1,
  17. "alterId": 100
  18. }`), user)
  19. assert.Error(err).IsNil()
  20. assert.String(user.ID).Equals("96edb838-6d68-42ef-a933-25f7ac3a9d09")
  21. assert.Byte(byte(user.Level)).Equals(1)
  22. }
  23. func TestInvalidUserJson(t *testing.T) {
  24. v2testing.Current(t)
  25. user := new(User)
  26. err := json.Unmarshal([]byte(`{"id": 1234}`), user)
  27. assert.Error(err).IsNotNil()
  28. }
  29. func TestInvalidIdJson(t *testing.T) {
  30. v2testing.Current(t)
  31. user := new(User)
  32. err := json.Unmarshal([]byte(`{"id": "1234"}`), user)
  33. assert.Error(err).IsNotNil()
  34. }