Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add check for empty deviceID in target URI template evaluation
- Loading branch information
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package wire | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
"text/template" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDPOPOptions_EvaluateTarget(t *testing.T) { | ||
tu := "http://wire.com:15958/clients/{{.DeviceID}}/access-token" | ||
target, err := template.New("DeviceID").Parse(tu) | ||
require.NoError(t, err) | ||
fail := "https://acme.elna.wire.link/clients/{{.DeviceId}}/access-token" // | ||
failTarget, err := template.New("DeviceID").Parse(fail) | ||
require.NoError(t, err) | ||
type fields struct { | ||
target *template.Template | ||
} | ||
type args struct { | ||
deviceID string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want string | ||
expectedErr error | ||
}{ | ||
{ | ||
name: "ok", fields: fields{target: target}, args: args{deviceID: "deviceID"}, want: "http://wire.com:15958/clients/deviceID/access-token", | ||
}, | ||
{ | ||
name: "fail/empty", fields: fields{target: target}, args: args{deviceID: ""}, expectedErr: errors.New("deviceID must not be empty"), | ||
}, | ||
{ | ||
name: "fail/template", fields: fields{target: failTarget}, args: args{deviceID: "bla"}, expectedErr: errors.New(`failed executing dpop template: template: DeviceID:1:38: executing "DeviceID" at <.DeviceId>: can't evaluate field DeviceId in type struct { DeviceID string }`), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
o := &DPOPOptions{ | ||
target: tt.fields.target, | ||
} | ||
got, err := o.EvaluateTarget(tt.args.deviceID) | ||
if tt.expectedErr != nil { | ||
assert.EqualError(t, err, tt.expectedErr.Error()) | ||
assert.Empty(t, got) | ||
return | ||
} | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |