Skip to content

Commit

Permalink
Make samlbinding tests easier to modify
Browse files Browse the repository at this point in the history
  • Loading branch information
tung2744 committed Aug 20, 2024
1 parent 075b0cf commit 1da4bb6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
9 changes: 6 additions & 3 deletions pkg/lib/saml/samlbinding/http_post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package samlbinding_test

import (
"bytes"
"encoding/base64"
"io"
"net/http"
"net/url"
Expand All @@ -19,7 +20,7 @@ func TestSAMLBindingHTTPPost(t *testing.T) {
req.URL = &url.URL{}
q := url.Values{}
relayState := "testrelaystate"
/*
samlRequestXML := `
<samlp:AuthnRequest
xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
Expand All @@ -32,8 +33,10 @@ func TestSAMLBindingHTTPPost(t *testing.T) {
Version="2.0"
><saml:Issuer>IAMShowcase</saml:Issuer></samlp:AuthnRequest
>
*/
samlRequest := "PHNhbWxwOkF1dGhuUmVxdWVzdA0KCXhtbG5zOnNhbWxwPSJ1cm46b2FzaXM6bmFtZXM6dGM6U0FNTDoyLjA6cHJvdG9jb2wiDQoJeG1sbnM6c2FtbD0idXJuOm9hc2lzOm5hbWVzOnRjOlNBTUw6Mi4wOmFzc2VydGlvbiINCglGb3JjZUF1dGhuPSJmYWxzZSINCglJRD0iYTUwNjA0NTM0MGRmNGUyMjkwNjQwZGFmMTg5ZjRiNTE3N2Y2OTE4MjMiDQoJSXNzdWVJbnN0YW50PSIyMDI0LTA4LTE2VDA4OjI1OjU5WiINCglEZXN0aW5hdGlvbj0iaHR0cDovL2xvY2FsaG9zdDozMDAwL3NhbWwyL2xvZ2luL3NwMSINCglBc3NlcnRpb25Db25zdW1lclNlcnZpY2VVUkw9Imh0dHA6Ly9leGFtcGxlLmNvbS9hY3MiDQoJUHJvdG9jb2xCaW5kaW5nPSJ1cm46b2FzaXM6bmFtZXM6dGM6U0FNTDoyLjA6YmluZGluZ3M6SFRUUC1QT1NUIg0KCVZlcnNpb249IjIuMCINCgk+PHNhbWw6SXNzdWVyPklBTVNob3djYXNlPC9zYW1sOklzc3Vlcj48L3NhbWxwOkF1dGhuUmVxdWVzdA0KPg=="
`
base64EncodedRequest := base64.StdEncoding.EncodeToString([]byte(samlRequestXML))

samlRequest := base64EncodedRequest
q.Add("RelayState", relayState)
q.Add("SAMLRequest", samlRequest)
bodyStr := q.Encode()
Expand Down
18 changes: 14 additions & 4 deletions pkg/lib/saml/samlbinding/http_redirect_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package samlbinding_test

import (
"bytes"
"compress/flate"
"encoding/base64"
"net/http"
"net/url"
"testing"
Expand All @@ -17,7 +20,7 @@ func TestSAMLBindingHTTPRedirect(t *testing.T) {
req.URL = &url.URL{}
q := url.Values{}
relayState := "testrelaystate"
/*
samlRequestXML := `
<samlp:AuthnRequest
xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
Expand All @@ -30,10 +33,17 @@ func TestSAMLBindingHTTPRedirect(t *testing.T) {
Version="2.0"
><saml:Issuer>IAMShowcase</saml:Issuer></samlp:AuthnRequest
>
*/
samlRequest := "fZFRT8IwFIXfTfwPZO+wrmxjaxjJlBhJIBI2ffCtlDu2pGtnb6f8fEdRoz7w1t57vttzbufIW9mxvLe12sFbD2hvb0ajUysVMtfLvN4opjk2yBRvAZkVrMg3a0YnhHVGWy209P5S1yGOCMY2WjnqQRsBzkDmVVwiuOpqmXk8IjEJo2lIDlUIlKYkHo68CpK0CvdRMJtVcRokdHohEHtYKbRc2cyjhIZjkoyDuCQJoxGL0lcnWw4RG8XPz2debW3HfF9qwWWt0bIpIcQ/J6BD8dgoH7vAYfm353utsG/BFGDeGwHPu/XPGDjxtpMwEbr1uUDHbb8WdNeoQ6OO1xezv4iQPZbldrx9Kko34wUMOruDxhUW87NF5hKbxSrfFLX+EBxh7v9uXG7/f3fxCQ=="
`
compressedRequestBuffer := &bytes.Buffer{}
writer, err := flate.NewWriter(compressedRequestBuffer, 9)
So(err, ShouldBeNil)
_, err = writer.Write([]byte(samlRequestXML))
So(err, ShouldBeNil)
err = writer.Close()
So(err, ShouldBeNil)
base64EncodedRequest := base64.StdEncoding.EncodeToString(compressedRequestBuffer.Bytes())
q.Add("RelayState", relayState)
q.Add("SAMLRequest", samlRequest)
q.Add("SAMLRequest", base64EncodedRequest)
req.URL.RawQuery = q.Encode()
result, err := samlbinding.SAMLBindingHTTPRedirectParse(req)
So(err, ShouldBeNil)
Expand Down

0 comments on commit 1da4bb6

Please sign in to comment.