|
1 | 1 | package steam_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
4 | 7 | "os"
|
5 | 8 | "testing"
|
6 | 9 |
|
@@ -50,6 +53,61 @@ func Test_SessionFromJSON(t *testing.T) {
|
50 | 53 | a.Equal(s.ResponseNonce, "2016-03-13T16:56:30ZJ8tlKVquwHi9ZSPV4ElU5PY2dmI=")
|
51 | 54 | }
|
52 | 55 |
|
| 56 | +func Test_FetchUser(t *testing.T) { |
| 57 | + apiUserSummaryPath := "/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s" |
| 58 | + |
| 59 | + t.Parallel() |
| 60 | + a := assert.New(t) |
| 61 | + p := provider() |
| 62 | + session, err := p.UnmarshalSession(`{"AuthURL":"https://steamcommunity.com/openid/login?openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.realm=%3A%2F%2F&openid.return_to=%2Ffoo","SteamID":"1234567890","CallbackURL":"http://localhost:3030/","ResponseNonce":"2016-03-13T16:56:30ZJ8tlKVquwHi9ZSPV4ElU5PY2dmI="}`) |
| 63 | + a.NoError(err) |
| 64 | + |
| 65 | + expectedPath := fmt.Sprintf(apiUserSummaryPath, p.APIKey, "1234567890") |
| 66 | + |
| 67 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 68 | + a.Equal("application/json", r.Header.Get("Accept")) |
| 69 | + a.Equal(http.MethodGet, r.Method) |
| 70 | + a.Equal(expectedPath, r.URL.RequestURI()) |
| 71 | + w.Write([]byte(testUserSummaryBody)) |
| 72 | + })) |
| 73 | + |
| 74 | + p.SummaryURL = ts.URL + apiUserSummaryPath |
| 75 | + |
| 76 | + user, err := p.FetchUser(session) |
| 77 | + a.NoError(err) |
| 78 | + a.Equal("76561197960435530", user.UserID) |
| 79 | + a.Equal("Robin", user.NickName) |
| 80 | + a.Equal("Robin Walker", user.Name) |
| 81 | + a.Equal("https://avatars.steamstatic.com/81b5478529dce13bf24b55ac42c1af7058aaf7a9_full.jpg", user.AvatarURL) |
| 82 | + a.Equal("No email is provided by the Steam API", user.Email) |
| 83 | + a.Equal("No description is provided by the Steam API", user.Description) |
| 84 | + a.Equal("WA, US", user.Location) |
| 85 | + a.Len(user.RawData, 6) |
| 86 | + a.Equal("76561197960435530", user.RawData["steamid"]) |
| 87 | + a.Equal("Robin", user.RawData["personaname"]) |
| 88 | + a.Equal("Robin Walker", user.RawData["realname"]) |
| 89 | + a.Equal("https://avatars.steamstatic.com/81b5478529dce13bf24b55ac42c1af7058aaf7a9_full.jpg", user.RawData["avatarfull"]) |
| 90 | + a.Equal("US", user.RawData["loccountrycode"]) |
| 91 | + a.Equal("WA", user.RawData["locstatecode"]) |
| 92 | +} |
| 93 | + |
53 | 94 | func provider() *steam.Provider {
|
54 | 95 | return steam.New(os.Getenv("STEAM_KEY"), "/foo")
|
55 | 96 | }
|
| 97 | + |
| 98 | +// Extracted from: https://developer.valvesoftware.com/wiki/Steam_Web_API |
| 99 | +var testUserSummaryBody = `{ |
| 100 | + "response": { |
| 101 | + "players": [ |
| 102 | + { |
| 103 | + "steamid": "76561197960435530", |
| 104 | + "personaname": "Robin", |
| 105 | + "realname": "Robin Walker", |
| 106 | + "avatarfull": "https://avatars.steamstatic.com/81b5478529dce13bf24b55ac42c1af7058aaf7a9_full.jpg", |
| 107 | + "loccountrycode": "US", |
| 108 | + "locstatecode": "WA" |
| 109 | + } |
| 110 | + ] |
| 111 | + |
| 112 | + } |
| 113 | +}` |
0 commit comments