Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 12 #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions har_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ var (
multipageExample string
//go:embed test/reference/plantuml/initiator-example.puml
initiatorExample string
//go:embed test/reference/plantuml/some-initiator-example.puml
someInitiatorExample string
)

func TestCreatesHarFromGooglePage(t *testing.T) {
Expand Down Expand Up @@ -172,6 +174,54 @@ func TestDrawHar_InitiatorTypes(t *testing.T) {
require.Equal(t, output.String(), initiatorExample)
}

// This test ensures that the legend does only render the initiator types which
// were actually used in the HAR file
func TestDrawHar_SomeInitiatorTypes(t *testing.T) {
har := hoofli.Har{
Log: hoofli.Log{
Pages: []hoofli.Page{{
ID: "page-1",
Title: "Example",
}},
Entries: []hoofli.Entry{
{
// unspecified _initiator
Pageref: "page-1",
Request: hoofli.Request{
Method: "GET",
URL: "https://example.com/page-1",
},
Response: hoofli.Response{Status: 200},
},
{
Initiator: hoofli.Initiator{Type: "script"},
Pageref: "page-1",
Request: hoofli.Request{
Method: "GET",
URL: "https://example.com/page-1",
},
Response: hoofli.Response{Status: 200},
},
{
Initiator: hoofli.Initiator{Type: "other"},
Pageref: "page-1",
Request: hoofli.Request{
Method: "GET",
URL: "https://example.com/page-1",
},
Response: hoofli.Response{Status: 200},
},
},
},
}

var output bytes.Buffer
err := har.Draw(&output)

require.NoError(t, err)
require.Equal(t, output.String(), someInitiatorExample)
}

func TestEntriesURLFilter_FixedPattern(t *testing.T) {
entries := hoofli.Entries{
{Request: hoofli.Request{URL: "https://example.com/page-1"}},
Expand Down
28 changes: 25 additions & 3 deletions plantuml.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ func (h *Har) Draw(w io.Writer) error {
// initiatorTypesUsed stores the used initiator types for rendering the
// correct legend at the end. Using a map over a list gives us automatic
// deduplication.
initiatorTypesUsed := map[string]bool{}
initiatorTypesUsed := map[string]bool{
"script": false,
"renderer": false,
"other": false,
"": false,
}
initiatorTypeOrder := []string{"script", "renderer", "other"}
fmt.Fprintln(w, "@startuml")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "participant Browser")
Expand All @@ -37,9 +43,13 @@ func (h *Har) Draw(w io.Writer) error {
}
}
// add a note explaining the colors of the connections
if len(initiatorTypesUsed) > 1 {
if shouldRenderLegend(initiatorTypesUsed) {
fmt.Fprintf(w, "note over Browser: Connection color represents initiator type:")
for t, _ := range initiatorTypesUsed {
for _, t := range initiatorTypeOrder {
if initiatorTypesUsed[t] == false {
// skip unused initiator type
continue
}
color := InitiatorTypeToColor(t)
fmt.Fprintf(w, "\\n<font color=%s>%s (%s)</font>", color, t, color)
}
Expand Down Expand Up @@ -68,3 +78,15 @@ func InitiatorTypeToColor(strType string) string {
}
return out
}

// shouldRenderLegend decides if the legend explaining the initiator types
// should be rendered. It will decide to render the legend if at least one
// initiator type other than the "unspecified" type was used.
func shouldRenderLegend(initiatorTypesUsed map[string]bool) bool {
for initiatorType, wasUsed := range initiatorTypesUsed {
if initiatorType != "" && wasUsed {
return true
}
}
return false
}
39 changes: 39 additions & 0 deletions plantuml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,42 @@ func TestTypeToColor(t *testing.T) {
})
}
}

func Test_shouldRenderLegend(t *testing.T) {
type args struct {
initiatorTypesUsed map[string]bool
}
tests := []struct {
name string
args args
want bool
}{
{
name: "Only unspecified type used",
args: args{map[string]bool{
"script": false,
"renderer": false,
"other": false,
"": true,
}},
want: false,
},
{
name: "Unspecified and renderer used",
args: args{map[string]bool{
"script": false,
"renderer": true,
"other": false,
"": true,
}},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := shouldRenderLegend(tt.args.initiatorTypesUsed); got != tt.want {
t.Errorf("shouldRenderLegend() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion test/reference/plantuml/initiator-example.puml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Browser-[#blue]->"example.com" ++ : GET https://example.com/page-1
return 200
Browser-[#green]->"example.com" ++ : GET https://example.com/page-1
return 200
note over Browser: Connection color represents initiator type:\n<font color=black> (black)</font>\n<font color=red>script (red)</font>\n<font color=blue>renderer (blue)</font>\n<font color=green>other (green)</font>
note over Browser: Connection color represents initiator type:\n<font color=red>script (red)</font>\n<font color=blue>renderer (blue)</font>\n<font color=green>other (green)</font>
deactivate Browser

@enduml
16 changes: 16 additions & 0 deletions test/reference/plantuml/some-initiator-example.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@startuml

participant Browser

->Browser : Example
activate Browser
Browser-[#black]->"example.com" ++ : GET https://example.com/page-1
return 200
Browser-[#red]->"example.com" ++ : GET https://example.com/page-1
return 200
Browser-[#green]->"example.com" ++ : GET https://example.com/page-1
return 200
note over Browser: Connection color represents initiator type:\n<font color=red>script (red)</font>\n<font color=green>other (green)</font>
deactivate Browser

@enduml