2015-04-13 21:12:03 +00:00
package audit
import (
"bytes"
"testing"
"github.com/hashicorp/vault/logical"
2015-06-19 03:14:20 +00:00
"errors"
2015-04-13 21:12:03 +00:00
)
func TestFormatJSON_formatRequest ( t * testing . T ) {
cases := map [ string ] struct {
Auth * logical . Auth
Req * logical . Request
2015-06-19 03:14:20 +00:00
Err error
2015-04-13 21:12:03 +00:00
Result string
} {
"auth, request" : {
& logical . Auth { ClientToken : "foo" , Policies : [ ] string { "root" } } ,
& logical . Request {
Operation : logical . WriteOperation ,
Path : "/foo" ,
2015-06-19 03:14:20 +00:00
Connection : & logical . Connection {
RemoteAddr : "127.0.0.1" ,
} ,
2015-04-13 21:12:03 +00:00
} ,
2015-06-19 03:14:20 +00:00
errors . New ( "this is an error" ) ,
2015-04-13 21:12:03 +00:00
testFormatJSONReqBasicStr ,
} ,
}
for name , tc := range cases {
var buf bytes . Buffer
var format FormatJSON
2015-06-19 03:14:20 +00:00
if err := format . FormatRequest ( & buf , tc . Auth , tc . Req , tc . Err ) ; err != nil {
2015-04-13 21:12:03 +00:00
t . Fatalf ( "bad: %s\nerr: %s" , name , err )
}
if buf . String ( ) != tc . Result {
t . Fatalf (
"bad: %s\nResult:\n\n%s\n\nExpected:\n\n%s" ,
name , buf . String ( ) , tc . Result )
}
}
}
2015-06-19 03:14:20 +00:00
const testFormatJSONReqBasicStr = ` { "type" : "request" , "auth" : { "display_name" : "" , "policies" : [ "root" ] , "metadata" : null } , "request" : { "operation" : "write" , "path" : "/foo" , "data" : null , "remote_address" : "127.0.0.1" } , "error" : "this is an error" }
2015-04-13 21:12:03 +00:00
`