Ejemplo n.º 1
0
func TestTwitterClient_Stream(t *testing.T) {
	assert := wcg.NewAssert(t)
	NUM_TWEETS_IN_STREAM := 5
	httptest.StartMockServer("./dummy", func(ms *httptest.MockServer) {
		ms.Routes().Get("/1.1/statuses/filter.json", func(res *wcg.Response, req *wcg.Request) {
			data, _ := json.Marshal(map[string]interface{}{
				"id_str":     "123",
				"text":       "test text",
				"created_at": "Thu Sep 24 13:26:59 +0000 2015",
			})
			for i := 0; i < NUM_TWEETS_IN_STREAM; i++ {
				res.WriteString(string(data) + "\r\n")
				res.WriteString("\r\n") // send empty (optional)
				res.Flush()
				time.Sleep(10 * time.Millisecond)
			}
			res.Close()
		})
		client := NewTwitterClient("", "", nil)
		client.StreamingEndpoint = ms.BaseUrl()
		client.Token = BearerToken("TestToken")
		ts, err := client.Stream(&StreamingParams{})
		assert.Nil(err, "Stream should not return an error")

		var tweet *Tweet
		ch := ts.GetStreamChannel()
		num_received := 0
		for tweet = range ch {
			num_received += 1
		}
		assert.EqInt(NUM_TWEETS_IN_STREAM+1, num_received, "The number of tweets is incorrect.")
		assert.Nil(tweet, "The end of stream should be nil")
	})

}
Ejemplo n.º 2
0
func TestTwitterClient_GetAppToken_NonJsonResponse(t *testing.T) {
	assert := wcg.NewAssert(t)
	httptest.StartMockServer("./dummy", func(ms *httptest.MockServer) {
		ms.Routes().Post("/oauth2/token", func(res *wcg.Response, req *wcg.Request) {
			res.WriteString("FOOO")
		})
		client := NewTwitterClient("", "", nil)
		client.Endpoint = ms.BaseUrl()
		_, err := client.GetAppToken()
		assert.NotNil(err, "GetAppToken should return an error when the server returns non JSON response.")
	})
}
Ejemplo n.º 3
0
func TestTwitterClient_GetAppToken_Non200Response(t *testing.T) {
	assert := wcg.NewAssert(t)
	httptest.StartMockServer("./dummy", func(ms *httptest.MockServer) {
		ms.Routes().Post("/oauth2/token", func(res *wcg.Response, req *wcg.Request) {
			res.WriteJsonWithStatus(404, nil, map[string]string{
				"access_token": "foo",
			})
		})
		client := NewTwitterClient("", "", nil)
		client.Endpoint = ms.BaseUrl()
		_, err := client.GetAppToken()
		assert.NotNil(err, "GetAppToken should return an error when the server returns non 200 code.")
	})
}
Ejemplo n.º 4
0
func TestTwitterClient_GetAppToken(t *testing.T) {
	assert := wcg.NewAssert(t)
	httptest.StartMockServer("./dummy", func(ms *httptest.MockServer) {
		ms.Routes().Post("/oauth2/token", func(res *wcg.Response, req *wcg.Request) {
			res.WriteJson(map[string]string{
				"access_token": "foo",
			})
		})
		client := NewTwitterClient("", "", nil)
		client.Endpoint = ms.BaseUrl()
		tok, err := client.GetAppToken()
		assert.Nil(err, "GetAppToken should not return an error.")
		assert.EqStr("foo", string(tok), "GetAppToken should return token value from the server response.")
	})
}