|
1 | 1 | package core |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "sync" |
5 | 6 | "testing" |
6 | 7 | "time" |
7 | 8 |
|
| 9 | + "github.com/chainreactors/logs" |
| 10 | + "github.com/chainreactors/parsers" |
| 11 | + "github.com/chainreactors/spray/core/baseline" |
8 | 12 | "github.com/chainreactors/spray/pkg" |
9 | 13 | ) |
10 | 14 |
|
@@ -55,3 +59,43 @@ func TestRecordStat_NilStat(t *testing.T) { |
55 | 59 | t.Fatalf("stats.Requests = %d, want 0 for nil stat", stats.Requests) |
56 | 60 | } |
57 | 61 | } |
| 62 | + |
| 63 | +func TestOutputHandlerSuppressesFuzzyChannelWithoutFuzzyFlag(t *testing.T) { |
| 64 | + var out bytes.Buffer |
| 65 | + oldLog := logs.Log |
| 66 | + logs.Log = logs.NewLogger(oldLog.Level) |
| 67 | + logs.Log.SetOutput(&out) |
| 68 | + t.Cleanup(func() { |
| 69 | + logs.Log = oldLog |
| 70 | + }) |
| 71 | + |
| 72 | + r := &Runner{ |
| 73 | + Option: &Option{}, |
| 74 | + OutputCh: make(chan *baseline.Baseline, 1), |
| 75 | + FuzzyCh: make(chan *baseline.Baseline, 1), |
| 76 | + OutWg: &sync.WaitGroup{}, |
| 77 | + } |
| 78 | + r.OutputHandler() |
| 79 | + |
| 80 | + r.OutWg.Add(1) |
| 81 | + r.FuzzyCh <- &baseline.Baseline{ |
| 82 | + SprayResult: &parsers.SprayResult{ |
| 83 | + UrlString: "http://example.com/fuzzy", |
| 84 | + Status: 400, |
| 85 | + BodyLength: 100, |
| 86 | + IsValid: true, |
| 87 | + IsFuzzy: true, |
| 88 | + Reason: pkg.ErrFuzzyCompareFailed.Error(), |
| 89 | + Source: parsers.CommonFileSource, |
| 90 | + }, |
| 91 | + } |
| 92 | + mustFinishCore(t, 2*time.Second, "fuzzy output was not drained", func() { |
| 93 | + r.OutWg.Wait() |
| 94 | + }) |
| 95 | + close(r.OutputCh) |
| 96 | + close(r.FuzzyCh) |
| 97 | + |
| 98 | + if got := out.String(); got != "" { |
| 99 | + t.Fatalf("fuzzy output without --fuzzy = %q, want empty", got) |
| 100 | + } |
| 101 | +} |
0 commit comments