func TestClientWithNewDevice(t *testing.T) { // a test server to represent AGO agoServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, r *http.Request) { test.Refute(t, r, nil) test.Expect(t, r.URL.Path, "/sharing/oauth2/registerDevice") test.Expect(t, r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") contents, _ := ioutil.ReadAll(r.Body) test.Refute(t, len(contents), 0) vals, _ := url.ParseQuery(string(contents)) test.Expect(t, len(vals), 2) test.Expect(t, vals.Get("client_id"), "good_client_id") test.Expect(t, vals.Get("f"), "json") fmt.Fprintln(res, `{"device":{"deviceID":"device_id","client_id":"good_client_id","apnsProdToken":null,"apnsSandboxToken":null,"gcmRegistrationId":null,"registered":1389531528000,"lastAccessed":1389531528000},"deviceToken":{"access_token":"good_access_token","expires_in":1799,"refresh_token":"good_refresh_token"}}`) })) defer agoServer.Close() // set the ago url to the url of our test server so we aren't hitting prod agoURLRestorer, err := test.Patch(defEnv, *testEnv("", agoServer.URL)) if err != nil { t.Error("Error during test setup: %s", err) } defer agoURLRestorer.Restore() client, err := NewDevice("good_client_id") test.Expect(t, err, nil) test.Refute(t, client, nil) }
func TestClientWithNewApplication(t *testing.T) { // a test server to represent AGO agoServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, r *http.Request) { test.Refute(t, r, nil) test.Expect(t, r.URL.Path, "/sharing/oauth2/token") test.Expect(t, r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") contents, _ := ioutil.ReadAll(r.Body) test.Refute(t, len(contents), 0) vals, _ := url.ParseQuery(string(contents)) test.Expect(t, len(vals), 4) test.Expect(t, vals.Get("client_id"), "good_client_id") test.Expect(t, vals.Get("f"), "json") test.Expect(t, vals.Get("grant_type"), "client_credentials") test.Expect(t, vals.Get("client_secret"), "good_client_secret") fmt.Fprintln(res, `{"access_token":"good_access_token","expires_in":7200}`) })) defer agoServer.Close() // set the ago url to the url of our test server so we aren't hitting prod agoURLRestorer, err := test.Patch(defEnv, *testEnv("", agoServer.URL)) if err != nil { t.Error("Error during test setup: %s", err) } defer agoURLRestorer.Restore() client, err := NewApplication("good_client_id", "good_client_secret") test.Expect(t, err, nil) test.Refute(t, client, nil) }