|
| 1 | +// html_test.go |
| 2 | +// Tests for HTML‐based plugin discovery functions. |
| 3 | + |
| 4 | +package scanner |
| 5 | + |
| 6 | +import ( |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "reflect" |
| 10 | + "sort" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | +) |
| 14 | + |
| 15 | +func sortedSlice(ss []string) []string { |
| 16 | + s := append([]string(nil), ss...) |
| 17 | + sort.Strings(s) |
| 18 | + return s |
| 19 | +} |
| 20 | + |
| 21 | +func TestExtractSlugsFromReader(t *testing.T) { |
| 22 | + tests := []struct { |
| 23 | + name string |
| 24 | + html string |
| 25 | + wantSlugs []string |
| 26 | + }{ |
| 27 | + { |
| 28 | + name: "Single plugin in href", |
| 29 | + html: `<html><head> |
| 30 | + <link rel="stylesheet" href="https://example.com/wp-content/plugins/pluginA/style.css" /> |
| 31 | + </head><body></body></html>`, |
| 32 | + wantSlugs: []string{"pluginA"}, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "Multiple plugins in href and src", |
| 36 | + html: `<html><body> |
| 37 | + <img src="/wp-content/plugins/pluginB/images/img.png" /> |
| 38 | + <a href="http://foo/wp-content/plugins/pluginC/file.php"></a> |
| 39 | + </body></html>`, |
| 40 | + wantSlugs: []string{"pluginB", "pluginC"}, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "Duplicate slugs and nested paths", |
| 44 | + html: `<html><body> |
| 45 | + <script src="/wp-content/plugins/pluginA/js/app.js"></script> |
| 46 | + <link href="/wp-content/plugins/pluginA/css/app.css" rel="stylesheet"> |
| 47 | + <img src="/some/other/path/pluginA/wp-content/plugins/pluginD/img.jpg"> |
| 48 | + </body></html>`, |
| 49 | + wantSlugs: []string{"pluginA", "pluginD"}, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "No plugin references", |
| 53 | + html: `<html><body><p>No plugins here</p></body></html>`, |
| 54 | + wantSlugs: []string{}, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "Malformed attributes", |
| 58 | + html: `<html><body> |
| 59 | + <a href="wp-content/plugins//style.css"></a> |
| 60 | + <a href="/wp-content/plugins/"></a> |
| 61 | + </body></html>`, |
| 62 | + wantSlugs: []string{}, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + for _, tt := range tests { |
| 67 | + t.Run(tt.name, func(t *testing.T) { |
| 68 | + dest := make(map[string]struct{}) |
| 69 | + err := extractSlugsFromReader(strings.NewReader(tt.html), dest) |
| 70 | + if err != nil { |
| 71 | + t.Fatalf("extractSlugsFromReader returned error: %v", err) |
| 72 | + } |
| 73 | + |
| 74 | + var got []string |
| 75 | + for slug := range dest { |
| 76 | + got = append(got, slug) |
| 77 | + } |
| 78 | + got = sortedSlice(got) |
| 79 | + want := sortedSlice(tt.wantSlugs) |
| 80 | + |
| 81 | + if !reflect.DeepEqual(got, want) { |
| 82 | + t.Errorf("extractSlugsFromReader = %v, want %v", got, want) |
| 83 | + } |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestDiscoverPluginsFromHTML(t *testing.T) { |
| 89 | + const sampleHTML = `<!DOCTYPE html> |
| 90 | +<html> |
| 91 | +<head> |
| 92 | + <link rel="stylesheet" href="/wp-content/plugins/pluginX/css/style.css"> |
| 93 | + <script src="http://host/wp-content/plugins/pluginY/js/app.js"></script> |
| 94 | +</head> |
| 95 | +<body> |
| 96 | + <img src="/wp-content/plugins/pluginZ/images/pic.png" alt="image"> |
| 97 | + <a href="/some/other/path"></a> |
| 98 | +</body> |
| 99 | +</html>` |
| 100 | + |
| 101 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 102 | + w.WriteHeader(http.StatusOK) |
| 103 | + _, _ = w.Write([]byte(sampleHTML)) |
| 104 | + })) |
| 105 | + defer ts.Close() |
| 106 | + |
| 107 | + slugs, err := discoverPluginsFromHTML(ts.URL, nil) |
| 108 | + if err != nil { |
| 109 | + t.Fatalf("discoverPluginsFromHTML returned error: %v", err) |
| 110 | + } |
| 111 | + |
| 112 | + got := sortedSlice(slugs) |
| 113 | + want := sortedSlice([]string{"pluginX", "pluginY", "pluginZ"}) |
| 114 | + |
| 115 | + if !reflect.DeepEqual(got, want) { |
| 116 | + t.Errorf("discoverPluginsFromHTML = %v, want %v", got, want) |
| 117 | + } |
| 118 | +} |
0 commit comments