...

Source file src/github.com/ory/fosite/authorize_validators_test.go

Documentation: github.com/ory/fosite

     1  /*
     2   * Copyright © 2017-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * @author		Aeneas Rekkas <aeneas+oss@aeneas.io>
    17   * @Copyright 	2017-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
    18   * @license 	Apache-2.0
    19   *
    20   */
    21  
    22  package fosite
    23  
    24  import (
    25  	"fmt"
    26  	"net/http"
    27  	"net/url"
    28  	"strings"
    29  	"testing"
    30  
    31  	"github.com/stretchr/testify/assert"
    32  	"github.com/stretchr/testify/require"
    33  )
    34  
    35  func TestValidateResponseTypes(t *testing.T) {
    36  	f := &Fosite{}
    37  	for k, tc := range []struct {
    38  		rt        string
    39  		art       []string
    40  		expectErr bool
    41  	}{
    42  		{
    43  			rt:        "code",
    44  			art:       []string{"token"},
    45  			expectErr: true,
    46  		},
    47  		{
    48  			rt:  "token",
    49  			art: []string{"token"},
    50  		},
    51  		{
    52  			rt:        "",
    53  			art:       []string{"token"},
    54  			expectErr: true,
    55  		},
    56  		{
    57  			rt:        "  ",
    58  			art:       []string{"token"},
    59  			expectErr: true,
    60  		},
    61  		{
    62  			rt:        "disable",
    63  			art:       []string{"token"},
    64  			expectErr: true,
    65  		},
    66  		{
    67  			rt:        "code token",
    68  			art:       []string{"token", "code"},
    69  			expectErr: true,
    70  		},
    71  		{
    72  			rt:  "code token",
    73  			art: []string{"token", "token code"},
    74  		},
    75  		{
    76  			rt:  "code token",
    77  			art: []string{"token", "code token"},
    78  		},
    79  		{
    80  			rt:        "code token",
    81  			art:       []string{"token", "code token id_token"},
    82  			expectErr: true,
    83  		},
    84  	} {
    85  		t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
    86  			r := &http.Request{Form: url.Values{"response_type": {tc.rt}}}
    87  			if tc.rt == "disable" {
    88  				r = &http.Request{Form: url.Values{}}
    89  			}
    90  			ar := NewAuthorizeRequest()
    91  			ar.Request.Client = &DefaultClient{ResponseTypes: tc.art}
    92  
    93  			err := f.validateResponseTypes(r, ar)
    94  			if tc.expectErr {
    95  				require.Error(t, err)
    96  			} else {
    97  				require.NoError(t, err)
    98  				assert.EqualValues(t, RemoveEmpty(strings.Split(tc.rt, " ")), ar.GetResponseTypes())
    99  			}
   100  		})
   101  	}
   102  }
   103  

View as plain text