github.com/qlik-oss/gopherciser@v0.18.6/connection/wsconnection_test.go (about) 1 package connection 2 3 import ( 4 "net/http" 5 "testing" 6 7 "github.com/goccy/go-json" 8 "github.com/qlik-oss/gopherciser/users" 9 ) 10 11 func TestWsConnection(t *testing.T) { 12 13 raw := `{ 14 "server" : "myhost", 15 "mode" : "ws", 16 "headers" : { 17 "static" : "staticstuff", 18 "user" : "{{.UserName}}", 19 "combined" : "{{.Directory}}\\\\{{.UserName}}" 20 } 21 }` 22 23 var connection ConnectionSettings 24 if err := json.Unmarshal([]byte(raw), &connection); err != nil { 25 t.Fatal("failed to unmarshal connectionsettings:", err) 26 } 27 28 if connection.Mode != WS { 29 t.Errorf("expected mode %d got %d", WS, connection.Mode) 30 } 31 32 if connection.Server != "myhost" { 33 t.Errorf("expected host<myhost>, got host<%s>", connection.Server) 34 } 35 36 data := &users.User{ 37 UserName: "user1", 38 Password: "password1", 39 Directory: "directory1", 40 } 41 42 header, err := connection.addReqHeaders(data, nil) 43 if err != nil { 44 t.Error(err) 45 } 46 validateHeaders(t, header) 47 48 header = make(http.Header, 1) 49 header.Set("extra", "extra1") 50 header, err = connection.addReqHeaders(data, header) 51 if err != nil { 52 t.Error(err) 53 } 54 validateHeaders(t, header) 55 extra := header.Get("extra") 56 if extra != "extra1" { 57 t.Errorf("unexpected extra header<%s>, expected<extra1>", extra) 58 } 59 } 60 61 func validateHeaders(t *testing.T, header http.Header) { 62 t.Helper() 63 64 static := header.Get("static") 65 if static != "staticstuff" { 66 t.Errorf("unexpected static header<%s>, expected<staticstuff>", static) 67 } 68 69 user := header.Get("user") 70 if user != "user1" { 71 t.Errorf("unexpected user header<%s>, expected<user1>", user) 72 } 73 74 combined := header.Get("combined") 75 if combined != "directory1\\\\user1" { 76 t.Errorf("unexpected combined header<%s>, expected<directory1\\\\user1>", combined) 77 } 78 }