コード例 #1
0
ファイル: token_server_test.go プロジェクト: raiqub/oauth
func TestClientGrant(t *testing.T) {
	adapter := oauthtest.NewTokenAdapter()
	svc := oauth.NewTokenService(adapter, oauth.GrantTypeClient)
	srv := NewTokenServer(svc)
	adapter.CustomValues[FormKeyCustom] = []string{FormValueCustom}

	gin.SetMode(gin.ReleaseMode)
	router := gin.New()
	router.RedirectTrailingSlash = true

	router.POST(ListenerEndpoint, srv.AccessTokenRequest)
	ts := httptest.NewServer(router)
	defer ts.Close()

	client := &http.Client{}
	form := url.Values{
		oauth.FormKeyGrantType: []string{oauth.GrantTypeClient},
		oauth.FormKeyScope:     []string{adapter.Scope},
		FormKeyCustom:          []string{FormValueCustom},
	}
	req, _ := http.NewRequest("POST", ts.URL+ListenerEndpoint,
		strings.NewReader(form.Encode()))
	req.SetBasicAuth(adapter.ClientID, adapter.ClientSecret)
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	resp, err := client.Do(req)
	if err != nil {
		t.Fatalf("Error posting data to server: %v", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		t.Errorf("Unexpected HTTP status: %s", resp.Status)
		strBody, _ := ioutil.ReadAll(resp.Body)
		t.Fatalf("Response body: %s", string(strBody))
	}

	var response oauth.TokenResponse
	if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
		t.Fatalf("Error parsing response: %v", err)
	}

	if response.AccessToken != adapter.AccessToken {
		t.Fatalf("Unexpected response: %#v", response)
	}
}
コード例 #2
0
ファイル: token_service_test.go プロジェクト: raiqub/oauth
func TestClientGrant(t *testing.T) {
	adapter := oauthtest.NewTokenAdapter()
	svc := oauth.NewTokenService(adapter, oauth.GrantTypeClient)

	context := oauth.TokenContext{
		GrantType:  oauth.GrantTypeClient,
		Scope:      adapter.Scope,
		HTTPUser:   adapter.ClientID,
		HTTPSecret: adapter.ClientSecret,
	}
	resp, jerr := svc.AccessTokenRequest(&context)
	if jerr != nil {
		t.Fatalf("Error trying to get client grant: %v", jerr.Description)
	}

	if resp.AccessToken != adapter.AccessToken {
		t.Errorf("Unexpected access token: %s", resp.AccessToken)
	}
}