diff --git a/har_test.go b/har_test.go
index fd4ed62..020fcd0 100644
--- a/har_test.go
+++ b/har_test.go
@@ -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) {
@@ -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"}},
diff --git a/plantuml.go b/plantuml.go
index deb5130..81c6e5d 100644
--- a/plantuml.go
+++ b/plantuml.go
@@ -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")
@@ -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%s (%s)", color, t, color)
}
@@ -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
+}
diff --git a/plantuml_test.go b/plantuml_test.go
index b43b95b..1854117 100644
--- a/plantuml_test.go
+++ b/plantuml_test.go
@@ -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)
+ }
+ })
+ }
+}
diff --git a/test/reference/plantuml/initiator-example.puml b/test/reference/plantuml/initiator-example.puml
index 938157f..1d05ccb 100644
--- a/test/reference/plantuml/initiator-example.puml
+++ b/test/reference/plantuml/initiator-example.puml
@@ -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 (black)\nscript (red)\nrenderer (blue)\nother (green)
+note over Browser: Connection color represents initiator type:\nscript (red)\nrenderer (blue)\nother (green)
deactivate Browser
@enduml
diff --git a/test/reference/plantuml/some-initiator-example.puml b/test/reference/plantuml/some-initiator-example.puml
new file mode 100644
index 0000000..efd3c19
--- /dev/null
+++ b/test/reference/plantuml/some-initiator-example.puml
@@ -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:\nscript (red)\nother (green)
+deactivate Browser
+
+@enduml