How to use Test method of parser Package

Best Gauge code snippet using parser.Test

cfg_parser_test.go

Source:cfg_parser_test.go Github

copy

Full Screen

...28 "regexp"29 "strings"30 "testing"31)32type customTestReceiverOutput struct {33 initCalled bool34 dataPassed string35 messageOutput string36 levelOutput LogLevel37 closed bool38 flushed bool39}40type customTestReceiver struct{ co *customTestReceiverOutput }41func (cr *customTestReceiver) ReceiveMessage(message string, level LogLevel, context LogContextInterface) error {42 cr.co.messageOutput = message43 cr.co.levelOutput = level44 return nil45}46func (cr *customTestReceiver) String() string {47 return fmt.Sprintf("custom data='%s'", cr.co.dataPassed)48}49func (cr *customTestReceiver) AfterParse(initArgs CustomReceiverInitArgs) error {50 cr.co = new(customTestReceiverOutput)51 cr.co.initCalled = true52 cr.co.dataPassed = initArgs.XmlCustomAttrs["test"]53 return nil54}55func (cr *customTestReceiver) Flush() {56 cr.co.flushed = true57}58func (cr *customTestReceiver) Close() error {59 cr.co.closed = true60 return nil61}62var re = regexp.MustCompile(`[^a-zA-Z0-9]+`)63func getTestFileName(testName, postfix string) string {64 if len(postfix) != 0 {65 return strings.ToLower(re.ReplaceAllString(testName, "_")) + "_" + postfix + "_test.log"66 }67 return strings.ToLower(re.ReplaceAllString(testName, "_")) + "_test.log"68}69var parserTests []parserTest70type parserTest struct {71 testName string72 config string73 expected *configForParsing //interface{}74 errorExpected bool75 parserConfig *CfgParseParams76}77func getParserTests() []parserTest {78 if parserTests == nil {79 parserTests = make([]parserTest, 0)80 testName := "Simple file output"81 testLogFileName := getTestFileName(testName, "")82 testConfig := `83 <seelog>84 <outputs>85 <file path="` + testLogFileName + `"/>86 </outputs>87 </seelog>88 `89 testExpected := new(configForParsing)90 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)91 testExpected.Exceptions = nil92 testfileWriter, _ := NewFileWriter(testLogFileName)93 testHeadSplitter, _ := NewSplitDispatcher(DefaultFormatter, []interface{}{testfileWriter})94 testExpected.LogType = asyncLooploggerTypeFromString95 testExpected.RootDispatcher = testHeadSplitter96 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})97 testName = "Filter dispatcher"98 testLogFileName = getTestFileName(testName, "")99 testConfig = `100 <seelog type="sync">101 <outputs>102 <filter levels="debug, info, critical">103 <file path="` + testLogFileName + `"/>104 </filter>105 </outputs>106 </seelog>107 `108 testExpected = new(configForParsing)109 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)110 testExpected.Exceptions = nil111 testfileWriter, _ = NewFileWriter(testLogFileName)112 testFilter, _ := NewFilterDispatcher(DefaultFormatter, []interface{}{testfileWriter}, DebugLvl, InfoLvl, CriticalLvl)113 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testFilter})114 testExpected.LogType = syncloggerTypeFromString115 testExpected.RootDispatcher = testHeadSplitter116 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})117 testName = "Console writer"118 testConfig = `119 <seelog type="sync">120 <outputs>121 <console />122 </outputs>123 </seelog>124 `125 testExpected = new(configForParsing)126 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)127 testExpected.Exceptions = nil128 testconsoleWriter, _ := NewConsoleWriter()129 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testconsoleWriter})130 testExpected.LogType = syncloggerTypeFromString131 testExpected.RootDispatcher = testHeadSplitter132 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})133 testName = "SMTP writer"134 testConfig = `135<seelog>136 <outputs>137 <smtp senderaddress="sa" sendername="sn" hostname="hn" hostport="123" username="un" password="up">138 <recipient address="ra1"/>139 <recipient address="ra2"/>140 <recipient address="ra3"/>141 <cacertdirpath path="cacdp1"/>142 <cacertdirpath path="cacdp2"/>143 </smtp>144 </outputs>145</seelog>146 `147 testExpected = new(configForParsing)148 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)149 testExpected.Exceptions = nil150 testSMTPWriter := NewSMTPWriter(151 "sa",152 "sn",153 []string{"ra1", "ra2", "ra3"},154 "hn",155 "123",156 "un",157 "up",158 []string{"cacdp1", "cacdp2"},159 DefaultSubjectPhrase,160 nil,161 )162 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testSMTPWriter})163 testExpected.LogType = asyncLooploggerTypeFromString164 testExpected.RootDispatcher = testHeadSplitter165 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})166 testName = "SMTP writer custom header and subject configuration"167 testConfig = `168<seelog>169 <outputs>170 <smtp senderaddress="sa" sendername="sn" hostname="hn" hostport="123" username="un" password="up" subject="ohlala">171 <recipient address="ra1"/>172 <cacertdirpath path="cacdp1"/>173 <header name="Priority" value="Urgent" />174 <header name="Importance" value="high" />175 <header name="Sensitivity" value="Company-Confidential" />176 <header name="Auto-Submitted" value="auto-generated" />177 </smtp>178 </outputs>179</seelog>180 `181 testExpected = new(configForParsing)182 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)183 testExpected.Exceptions = nil184 testSMTPWriter = NewSMTPWriter(185 "sa",186 "sn",187 []string{"ra1"},188 "hn",189 "123",190 "un",191 "up",192 []string{"cacdp1"},193 "ohlala",194 []string{"Priority: Urgent", "Importance: high", "Sensitivity: Company-Confidential", "Auto-Submitted: auto-generated"},195 )196 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testSMTPWriter})197 testExpected.LogType = asyncLooploggerTypeFromString198 testExpected.RootDispatcher = testHeadSplitter199 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})200 testName = "Default output"201 testConfig = `202 <seelog type="sync"/>203 `204 testExpected = new(configForParsing)205 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)206 testExpected.Exceptions = nil207 testconsoleWriter, _ = NewConsoleWriter()208 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testconsoleWriter})209 testExpected.LogType = syncloggerTypeFromString210 testExpected.RootDispatcher = testHeadSplitter211 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})212 testName = "Asyncloop behavior"213 testConfig = `214 <seelog type="asyncloop"/>215 `216 testExpected = new(configForParsing)217 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)218 testExpected.Exceptions = nil219 testconsoleWriter, _ = NewConsoleWriter()220 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testconsoleWriter})221 testExpected.LogType = asyncLooploggerTypeFromString222 testExpected.RootDispatcher = testHeadSplitter223 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})224 testName = "Asynctimer behavior"225 testConfig = `226 <seelog type="asynctimer" asyncinterval="101"/>227 `228 testExpected = new(configForParsing)229 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)230 testExpected.Exceptions = nil231 testconsoleWriter, _ = NewConsoleWriter()232 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testconsoleWriter})233 testExpected.LogType = asyncTimerloggerTypeFromString234 testExpected.LoggerData = asyncTimerLoggerData{101}235 testExpected.RootDispatcher = testHeadSplitter236 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})237 testName = "Rolling file writer size"238 testLogFileName = getTestFileName(testName, "")239 testConfig = `240 <seelog type="sync">241 <outputs>242 <rollingfile type="size" filename="` + testLogFileName + `" maxsize="100" maxrolls="5" />243 </outputs>244 </seelog>245 `246 testExpected = new(configForParsing)247 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)248 testExpected.Exceptions = nil249 testrollingFileWriter, _ := NewRollingFileWriterSize(testLogFileName, rollingArchiveNone, "", 100, 5, rollingNameModePostfix, false)250 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testrollingFileWriter})251 testExpected.LogType = syncloggerTypeFromString252 testExpected.RootDispatcher = testHeadSplitter253 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})254 testName = "Rolling file writer archive gzip"255 testLogFileName = getTestFileName(testName, "")256 testConfig = `257 <seelog type="sync">258 <outputs>259 <rollingfile type="size" filename="` + testLogFileName + `" maxsize="100" maxrolls="5" archivetype="gzip"/>260 </outputs>261 </seelog>`262 testExpected = new(configForParsing)263 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)264 testExpected.Exceptions = nil265 testrollingFileWriter, _ = NewRollingFileWriterSize(testLogFileName, rollingArchiveGzip, "log.tar.gz", 100, 5, rollingNameModePostfix, false)266 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testrollingFileWriter})267 testExpected.LogType = syncloggerTypeFromString268 testExpected.RootDispatcher = testHeadSplitter269 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})270 testName = "Rolling file writer archive zip"271 testLogFileName = getTestFileName(testName, "")272 testConfig = `273 <seelog type="sync">274 <outputs>275 <rollingfile type="size" filename="` + testLogFileName + `" maxsize="100" maxrolls="5" archivetype="zip"/>276 </outputs>277 </seelog>`278 testExpected = new(configForParsing)279 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)280 testExpected.Exceptions = nil281 testrollingFileWriter, _ = NewRollingFileWriterSize(testLogFileName, rollingArchiveZip, "log.zip", 100, 5, rollingNameModePostfix, false)282 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testrollingFileWriter})283 testExpected.LogType = syncloggerTypeFromString284 testExpected.RootDispatcher = testHeadSplitter285 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})286 testName = "Rolling file writer archive zip with specified path"287 testLogFileName = getTestFileName(testName, "")288 testConfig = `289 <seelog type="sync">290 <outputs>291 <rollingfile namemode="prefix" type="size" filename="` + testLogFileName + `" maxsize="100" maxrolls="5" archivetype="zip" archivepath="test.zip"/>292 </outputs>293 </seelog>`294 testExpected = new(configForParsing)295 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)296 testExpected.Exceptions = nil297 testrollingFileWriter, _ = NewRollingFileWriterSize(testLogFileName, rollingArchiveZip, "test.zip", 100, 5, rollingNameModePrefix, false)298 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testrollingFileWriter})299 testExpected.LogType = syncloggerTypeFromString300 testExpected.RootDispatcher = testHeadSplitter301 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})302 testName = "Rolling file writer archive zip exploded"303 testLogFileName = getTestFileName(testName, "")304 testConfig = `305 <seelog type="sync">306 <outputs>307 <rollingfile type="size" filename="` + testLogFileName + `" maxsize="100" maxrolls="5" archivetype="zip" archiveexploded="true"/>308 </outputs>309 </seelog>`310 testExpected = new(configForParsing)311 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)312 testExpected.Exceptions = nil313 testrollingFileWriter, _ = NewRollingFileWriterSize(testLogFileName, rollingArchiveZip, "old", 100, 5, rollingNameModePostfix, true)314 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testrollingFileWriter})315 testExpected.LogType = syncloggerTypeFromString316 testExpected.RootDispatcher = testHeadSplitter317 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})318 testName = "Rolling file writer archive zip exploded with specified path"319 testLogFileName = getTestFileName(testName, "")320 testConfig = `321 <seelog type="sync">322 <outputs>323 <rollingfile namemode="prefix" type="size" filename="` + testLogFileName + `" maxsize="100" maxrolls="5" archivetype="zip" archiveexploded="true" archivepath="test_old_logs"/>324 </outputs>325 </seelog>`326 testExpected = new(configForParsing)327 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)328 testExpected.Exceptions = nil329 testrollingFileWriter, _ = NewRollingFileWriterSize(testLogFileName, rollingArchiveZip, "test_old_logs", 100, 5, rollingNameModePrefix, true)330 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testrollingFileWriter})331 testExpected.LogType = syncloggerTypeFromString332 testExpected.RootDispatcher = testHeadSplitter333 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})334 testName = "Rolling file writer archive none"335 testLogFileName = getTestFileName(testName, "")336 testConfig = `337 <seelog type="sync">338 <outputs>339 <rollingfile namemode="postfix" type="size" filename="` + testLogFileName + `" maxsize="100" maxrolls="5" archivetype="none"/>340 </outputs>341 </seelog>`342 testExpected = new(configForParsing)343 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)344 testExpected.Exceptions = nil345 testrollingFileWriter, _ = NewRollingFileWriterSize(testLogFileName, rollingArchiveNone, "", 100, 5, rollingNameModePostfix, false)346 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testrollingFileWriter})347 testExpected.LogType = syncloggerTypeFromString348 testExpected.RootDispatcher = testHeadSplitter349 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})350 testName = "Rolling file writer date"351 testLogFileName = getTestFileName(testName, "")352 testConfig = `353 <seelog type="sync">354 <outputs>355 <rollingfile type="date" filename="` + testLogFileName + `" datepattern="2006-01-02T15:04:05Z07:00" />356 </outputs>357 </seelog>`358 testExpected = new(configForParsing)359 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)360 testExpected.Exceptions = nil361 testrollingFileWriterTime, _ := NewRollingFileWriterTime(testLogFileName, rollingArchiveNone, "", 0, "2006-01-02T15:04:05Z07:00", rollingNameModePostfix, false, false)362 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testrollingFileWriterTime})363 testExpected.LogType = syncloggerTypeFromString364 testExpected.RootDispatcher = testHeadSplitter365 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})366 testName = "Buffered writer"367 testLogFileName = getTestFileName(testName, "")368 testConfig = `369 <seelog type="sync">370 <outputs>371 <buffered size="100500" flushperiod="100">372 <rollingfile type="date" filename="` + testLogFileName + `" datepattern="2006-01-02T15:04:05Z07:00" />373 </buffered>374 </outputs>375 </seelog>`376 testExpected = new(configForParsing)377 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)378 testExpected.Exceptions = nil379 testrollingFileWriterTime, _ = NewRollingFileWriterTime(testLogFileName, rollingArchiveNone, "", 0, "2006-01-02T15:04:05Z07:00", rollingNameModePostfix, false, false)380 testbufferedWriter, _ := NewBufferedWriter(testrollingFileWriterTime, 100500, 100)381 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testbufferedWriter})382 testExpected.LogType = syncloggerTypeFromString383 testExpected.RootDispatcher = testHeadSplitter384 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})385 testName = "Inner splitter output"386 testLogFileName1 := getTestFileName(testName, "1")387 testLogFileName2 := getTestFileName(testName, "2")388 testLogFileName3 := getTestFileName(testName, "3")389 testConfig = `390 <seelog type="sync">391 <outputs>392 <file path="` + testLogFileName1 + `"/>393 <splitter>394 <file path="` + testLogFileName2 + `"/>395 <file path="` + testLogFileName3 + `"/>396 </splitter>397 </outputs>398 </seelog>399 `400 testExpected = new(configForParsing)401 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)402 testExpected.Exceptions = nil403 testfileWriter1, _ := NewFileWriter(testLogFileName2)404 testfileWriter2, _ := NewFileWriter(testLogFileName3)405 testInnerSplitter, _ := NewSplitDispatcher(DefaultFormatter, []interface{}{testfileWriter1, testfileWriter2})406 testfileWriter, _ = NewFileWriter(testLogFileName1)407 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testfileWriter, testInnerSplitter})408 testExpected.LogType = syncloggerTypeFromString409 testExpected.RootDispatcher = testHeadSplitter410 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})411 RegisterReceiver("custom-name-1", &customTestReceiver{})412 testName = "Custom receiver 1"413 testConfig = `414 <seelog type="sync">415 <outputs>416 <custom name="custom-name-1" data-test="set"/>417 </outputs>418 </seelog>419 `420 testExpected = new(configForParsing)421 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)422 testExpected.Exceptions = nil423 testCustomReceiver, _ := NewCustomReceiverDispatcher(DefaultFormatter, "custom-name-1", CustomReceiverInitArgs{424 XmlCustomAttrs: map[string]string{425 "test": "set",426 },427 })428 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testCustomReceiver})429 testExpected.LogType = syncloggerTypeFromString430 testExpected.RootDispatcher = testHeadSplitter431 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})432 testName = "Custom receiver 2"433 testConfig = `434 <seelog type="sync">435 <outputs>436 <custom name="custom-name-2" data-test="set2"/>437 </outputs>438 </seelog>439 `440 testExpected = new(configForParsing)441 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)442 testExpected.Exceptions = nil443 crec := &customTestReceiver{}444 cargs := CustomReceiverInitArgs{445 XmlCustomAttrs: map[string]string{446 "test": "set2",447 },448 }449 crec.AfterParse(cargs)450 testCustomReceiver2, _ := NewCustomReceiverDispatcherByValue(DefaultFormatter, crec, "custom-name-2", cargs)451 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testCustomReceiver2})452 testExpected.LogType = syncloggerTypeFromString453 testExpected.RootDispatcher = testHeadSplitter454 fnc := func(initArgs CustomReceiverInitArgs) (CustomReceiver, error) {455 return &customTestReceiver{}, nil456 }457 cfg := CfgParseParams{458 CustomReceiverProducers: map[string]CustomReceiverProducer{459 "custom-name-2": CustomReceiverProducer(fnc),460 },461 }462 testExpected.Params = &cfg463 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, &cfg})464 RegisterReceiver("-", &customTestReceiver{})465 testName = "Custom receiver 3"466 testConfig = `467 <seelog type="sync">468 <outputs>469 <custom name="-" data-test="set3"/>470 </outputs>471 </seelog>472 `473 testExpected = new(configForParsing)474 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)475 testExpected.Exceptions = nil476 creccustom := &customTestReceiver{}477 cargs3 := CustomReceiverInitArgs{478 XmlCustomAttrs: map[string]string{479 "test": "set3",480 },481 }482 creccustom.AfterParse(cargs3)483 testCustomReceiver, _ = NewCustomReceiverDispatcherByValue(DefaultFormatter, creccustom, "-", cargs3)484 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testCustomReceiver})485 testExpected.LogType = syncloggerTypeFromString486 testExpected.RootDispatcher = testHeadSplitter487 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})488 testName = "Custom receivers with formats"489 testConfig = `490 <seelog type="sync">491 <outputs>492 <custom name="custom-name-1" data-test="set1"/>493 <custom name="custom-name-1" data-test="set2"/>494 <custom name="custom-name-1" data-test="set3"/>495 </outputs>496 </seelog>497 `498 testExpected = new(configForParsing)499 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)500 testExpected.Exceptions = nil501 testCustomReceivers := make([]*customReceiverDispatcher, 3)502 for i := 0; i < 3; i++ {503 testCustomReceivers[i], _ = NewCustomReceiverDispatcher(DefaultFormatter, "custom-name-1", CustomReceiverInitArgs{504 XmlCustomAttrs: map[string]string{505 "test": fmt.Sprintf("set%d", i+1),506 },507 })508 }509 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testCustomReceivers[0], testCustomReceivers[1], testCustomReceivers[2]})510 testExpected.LogType = syncloggerTypeFromString511 testExpected.RootDispatcher = testHeadSplitter512 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})513 testName = "Format"514 testLogFileName = getTestFileName(testName, "")515 testConfig = `516 <seelog type="sync">517 <outputs formatid="dateFormat">518 <file path="` + testLogFileName + `"/>519 </outputs>520 <formats>521 <format id="dateFormat" format="%Level %Msg %File" />522 </formats>523 </seelog>524 `525 testExpected = new(configForParsing)526 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)527 testExpected.Exceptions = nil528 testfileWriter, _ = NewFileWriter(testLogFileName)529 testFormat, _ := NewFormatter("%Level %Msg %File")530 testHeadSplitter, _ = NewSplitDispatcher(testFormat, []interface{}{testfileWriter})531 testExpected.LogType = syncloggerTypeFromString532 testExpected.RootDispatcher = testHeadSplitter533 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})534 testName = "Format2"535 testLogFileName = getTestFileName(testName, "")536 testLogFileName1 = getTestFileName(testName, "1")537 testConfig = `538 <seelog type="sync">539 <outputs formatid="format1">540 <file path="` + testLogFileName + `"/>541 <file formatid="format2" path="` + testLogFileName1 + `"/>542 </outputs>543 <formats>544 <format id="format1" format="%Level %Msg %File" />545 <format id="format2" format="%l %Msg" />546 </formats>547 </seelog>548 `549 testExpected = new(configForParsing)550 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)551 testExpected.Exceptions = nil552 testfileWriter, _ = NewFileWriter(testLogFileName)553 testfileWriter1, _ = NewFileWriter(testLogFileName1)554 testFormat1, _ := NewFormatter("%Level %Msg %File")555 testFormat2, _ := NewFormatter("%l %Msg")556 formattedWriter, _ := NewFormattedWriter(testfileWriter1, testFormat2)557 testHeadSplitter, _ = NewSplitDispatcher(testFormat1, []interface{}{testfileWriter, formattedWriter})558 testExpected.LogType = syncloggerTypeFromString559 testExpected.RootDispatcher = testHeadSplitter560 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})561 testName = "Minlevel = warn"562 testConfig = `<seelog minlevel="warn"/>`563 testExpected = new(configForParsing)564 testExpected.Constraints, _ = NewMinMaxConstraints(WarnLvl, CriticalLvl)565 testExpected.Exceptions = nil566 testconsoleWriter, _ = NewConsoleWriter()567 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testconsoleWriter})568 testExpected.LogType = asyncLooploggerTypeFromString569 testExpected.RootDispatcher = testHeadSplitter570 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})571 testName = "Maxlevel = trace"572 testConfig = `<seelog maxlevel="trace"/>`573 testExpected = new(configForParsing)574 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, TraceLvl)575 testExpected.Exceptions = nil576 testconsoleWriter, _ = NewConsoleWriter()577 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testconsoleWriter})578 testExpected.LogType = asyncLooploggerTypeFromString579 testExpected.RootDispatcher = testHeadSplitter580 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})581 testName = "Level between info and error"582 testConfig = `<seelog minlevel="info" maxlevel="error"/>`583 testExpected = new(configForParsing)584 testExpected.Constraints, _ = NewMinMaxConstraints(InfoLvl, ErrorLvl)585 testExpected.Exceptions = nil586 testconsoleWriter, _ = NewConsoleWriter()587 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testconsoleWriter})588 testExpected.LogType = asyncLooploggerTypeFromString589 testExpected.RootDispatcher = testHeadSplitter590 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})591 testName = "Off with minlevel"592 testConfig = `<seelog minlevel="off"/>`593 testExpected = new(configForParsing)594 testExpected.Constraints, _ = NewOffConstraints()595 testExpected.Exceptions = nil596 testconsoleWriter, _ = NewConsoleWriter()597 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testconsoleWriter})598 testExpected.LogType = asyncLooploggerTypeFromString599 testExpected.RootDispatcher = testHeadSplitter600 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})601 testName = "Off with levels"602 testConfig = `<seelog levels="off"/>`603 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})604 testName = "Levels list"605 testConfig = `<seelog levels="debug, info, critical"/>`606 testExpected = new(configForParsing)607 testExpected.Constraints, _ = NewListConstraints([]LogLevel{608 DebugLvl, InfoLvl, CriticalLvl})609 testExpected.Exceptions = nil610 testconsoleWriter, _ = NewConsoleWriter()611 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testconsoleWriter})612 testExpected.LogType = asyncLooploggerTypeFromString613 testExpected.RootDispatcher = testHeadSplitter614 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})615 testName = "Errors #1"616 testConfig = `<seelog minlevel="debug" minlevel="trace"/>`617 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})618 testName = "Errors #2"619 testConfig = `<seelog minlevel="error" maxlevel="debug"/>`620 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})621 testName = "Errors #3"622 testConfig = `<seelog maxlevel="debug" maxlevel="trace"/>`623 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})624 testName = "Errors #4"625 testConfig = `<seelog maxlevel="off"/>`626 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})627 testName = "Errors #5"628 testConfig = `<seelog minlevel="off" maxlevel="trace"/>`629 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})630 testName = "Errors #6"631 testConfig = `<seelog minlevel="warn" maxlevel="error" levels="debug"/>`632 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})633 testName = "Errors #7"634 testConfig = `<not_seelog/>`635 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})636 testName = "Errors #8"637 testConfig = `<seelog levels="warn, debug, test"/>`638 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})639 testName = "Errors #9"640 testConfig = `<seelog levels=""/>`641 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})642 testName = "Errors #10"643 testConfig = `<seelog levels="off" something="abc"/>`644 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})645 testName = "Errors #11"646 testConfig = `<seelog><output/></seelog>`647 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})648 testName = "Errors #12"649 testConfig = `<seelog><outputs/><outputs/></seelog>`650 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})651 testName = "Errors #13"652 testConfig = `<seelog><exceptions/></seelog>`653 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})654 testName = "Errors #14"655 testConfig = `<seelog><formats/></seelog>`656 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})657 testName = "Errors #15"658 testConfig = `<seelog><outputs><splitter/></outputs></seelog>`659 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})660 testName = "Errors #16"661 testConfig = `<seelog><outputs><filter/></outputs></seelog>`662 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})663 testName = "Errors #17"664 testLogFileName = getTestFileName(testName, "")665 testConfig = `<seelog><outputs><file path="` + testLogFileName + `"><something/></file></outputs></seelog>`666 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})667 testName = "Errors #18"668 testConfig = `<seelog><outputs><buffered size="100500" flushperiod="100"/></outputs></seelog>`669 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})670 testName = "Errors #19"671 testConfig = `<seelog><outputs></outputs></seelog>`672 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})673 testName = "Exceptions: restricting"674 testConfig =675 `676 <seelog type="sync">677 <exceptions>678 <exception funcpattern="Test*" filepattern="someFile.go" minlevel="off"/>679 </exceptions>680 </seelog>681 `682 testExpected = new(configForParsing)683 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)684 listConstraint, _ := NewOffConstraints()685 exception, _ := NewLogLevelException("Test*", "someFile.go", listConstraint)686 testExpected.Exceptions = []*LogLevelException{exception}687 testconsoleWriter, _ = NewConsoleWriter()688 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testconsoleWriter})689 testExpected.LogType = syncloggerTypeFromString690 testExpected.RootDispatcher = testHeadSplitter691 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})692 testName = "Exceptions: allowing #1"693 testConfig =694 `695 <seelog type="sync" levels="error">696 <exceptions>697 <exception filepattern="testfile.go" minlevel="trace"/>698 </exceptions>699 </seelog>700 `701 testExpected = new(configForParsing)702 testExpected.Constraints, _ = NewListConstraints([]LogLevel{ErrorLvl})703 minMaxConstraint, _ := NewMinMaxConstraints(TraceLvl, CriticalLvl)704 exception, _ = NewLogLevelException("*", "testfile.go", minMaxConstraint)705 testExpected.Exceptions = []*LogLevelException{exception}706 testconsoleWriter, _ = NewConsoleWriter()707 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testconsoleWriter})708 testExpected.LogType = syncloggerTypeFromString709 testExpected.RootDispatcher = testHeadSplitter710 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})711 testName = "Exceptions: allowing #2"712 testConfig = `713 <seelog type="sync" levels="off">714 <exceptions>715 <exception filepattern="testfile.go" minlevel="warn"/>716 </exceptions>717 </seelog>718 `719 testExpected = new(configForParsing)720 testExpected.Constraints, _ = NewOffConstraints()721 minMaxConstraint, _ = NewMinMaxConstraints(WarnLvl, CriticalLvl)722 exception, _ = NewLogLevelException("*", "testfile.go", minMaxConstraint)723 testExpected.Exceptions = []*LogLevelException{exception}724 testconsoleWriter, _ = NewConsoleWriter()725 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testconsoleWriter})726 testExpected.LogType = syncloggerTypeFromString727 testExpected.RootDispatcher = testHeadSplitter728 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})729 testName = "Predefined formats"730 formatID := predefinedPrefix + "xml-debug-short"731 testConfig = `732 <seelog type="sync">733 <outputs formatid="` + formatID + `">734 <console />735 </outputs>736 </seelog>`737 testExpected = new(configForParsing)738 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)739 testExpected.Exceptions = nil740 testconsoleWriter, _ = NewConsoleWriter()741 testFormat, _ = predefinedFormats[formatID]742 testHeadSplitter, _ = NewSplitDispatcher(testFormat, []interface{}{testconsoleWriter})743 testExpected.LogType = syncloggerTypeFromString744 testExpected.RootDispatcher = testHeadSplitter745 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})746 testName = "Predefined formats redefine"747 testLogFileName = getTestFileName(testName, "")748 formatID = predefinedPrefix + "xml-debug-short"749 testConfig = `750 <seelog type="sync">751 <outputs formatid="` + formatID + `">752 <file path="` + testLogFileName + `"/>753 </outputs>754 <formats>755 <format id="` + formatID + `" format="%Level %Msg %File" />756 </formats>757 </seelog>`758 testExpected = new(configForParsing)759 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)760 testExpected.Exceptions = nil761 testfileWriter, _ = NewFileWriter(testLogFileName)762 testFormat, _ = NewFormatter("%Level %Msg %File")763 testHeadSplitter, _ = NewSplitDispatcher(testFormat, []interface{}{testfileWriter})764 testExpected.LogType = syncloggerTypeFromString765 testExpected.RootDispatcher = testHeadSplitter766 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})767 testName = "Conn writer 1"768 testConfig = `769 <seelog type="sync">770 <outputs>771 <conn net="tcp" addr=":8888" />772 </outputs>773 </seelog>`774 testExpected = new(configForParsing)775 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)776 testExpected.Exceptions = nil777 testConnWriter := NewConnWriter("tcp", ":8888", false)778 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testConnWriter})779 testExpected.LogType = syncloggerTypeFromString780 testExpected.RootDispatcher = testHeadSplitter781 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})782 testName = "Conn writer 2"783 testConfig = `784 <seelog type="sync">785 <outputs>786 <conn net="tcp" addr=":8888" reconnectonmsg="true" />787 </outputs>788 </seelog>`789 testExpected = new(configForParsing)790 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)791 testExpected.Exceptions = nil792 testConnWriter = NewConnWriter("tcp", ":8888", true)793 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testConnWriter})794 testExpected.LogType = syncloggerTypeFromString795 testExpected.RootDispatcher = testHeadSplitter796 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})797 testName = "Errors #11"798 testConfig = `799 <seelog type="sync"><exceptions>800 <exception filepattern="testfile.go" minlevel="trace"/>801 <exception filepattern="testfile.go" minlevel="warn"/>802 </exceptions></seelog>`803 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})804 testName = "Errors #12"805 testConfig = `806 <seelog type="sync"><exceptions>807 <exception filepattern="!@+$)!!%&@(^$" minlevel="trace"/>808 </exceptions></seelog>`809 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})810 testName = "Errors #13"811 testConfig = `812 <seelog type="sync"><exceptions>813 <exception filepattern="*" minlevel="unknown"/>814 </exceptions></seelog>`815 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})816 testName = "Errors #14"817 testConfig = `818 <seelog type="sync" levels=”off”>819 <exceptions>820 <exception filepattern="testfile.go" minlevel="off"/>821 </exceptions>822 </seelog>823 `824 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})825 testName = "Errors #15"826 testConfig = `827 <seelog type="sync" levels=”trace”>828 <exceptions>829 <exception filepattern="testfile.go" levels="trace"/>830 </exceptions>831 </seelog>832 `833 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})834 testName = "Errors #16"835 testConfig = `836 <seelog type="sync" minlevel=”trace”>837 <exceptions>838 <exception filepattern="testfile.go" minlevel="trace"/>839 </exceptions>840 </seelog>841 `842 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})843 testName = "Errors #17"844 testConfig = `845 <seelog type="sync" minlevel=”trace”>846 <exceptions>847 <exception filepattern="testfile.go" minlevel="warn"/>848 </exceptions>849 <exceptions>850 <exception filepattern="testfile.go" minlevel="warn"/>851 </exceptions>852 </seelog>853 `854 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})855 testName = "Errors #18"856 testConfig = `857 <seelog type="sync" minlevel=”trace”>858 <exceptions>859 <exception filepattern="testfile.go"/>860 </exceptions>861 </seelog>862 `863 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})864 testName = "Errors #19"865 testConfig = `866 <seelog type="sync" minlevel=”trace”>867 <exceptions>868 <exception minlevel="warn"/>869 </exceptions>870 </seelog>871 `872 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})873 testName = "Errors #20"874 testConfig = `875 <seelog type="sync" minlevel=”trace”>876 <exceptions>877 <exception/>878 </exceptions>879 </seelog>880 `881 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})882 testName = "Errors #21"883 testConfig = `884 <seelog>885 <outputs>886 <splitter>887 </splitter>888 </outputs>889 </seelog>890 `891 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})892 testName = "Errors #22"893 testConfig = `894 <seelog type="sync">895 <outputs>896 <filter levels="debug, info, critical">897 </filter>898 </outputs>899 </seelog>900 `901 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})902 testName = "Errors #23"903 testConfig = `904 <seelog type="sync">905 <outputs>906 <buffered size="100500" flushperiod="100">907 </buffered>908 </outputs>909 </seelog>910 `911 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})912 testName = "Errors #24"913 testLogFileName = getTestFileName(testName, "")914 testConfig = `915 <seelog type="sync">916 <outputs>917 <buffered size="100500" flushperiod="100">918 <rollingfile type="date" filename="` + testLogFileName + `" datepattern="2006-01-02T15:04:05Z07:00" formatid="testFormat"/>919 </buffered>920 </outputs>921 <formats>922 <format id="testFormat" format="%Level %Msg %File 123" />923 </formats>924 </seelog>925 `926 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})927 testName = "Errors #25"928 testLogFileName = getTestFileName(testName, "")929 testConfig = `930 <seelog type="sync">931 <outputs>932 <outputs>933 <file path="` + testLogFileName + `"/>934 </outputs>935 <outputs>936 <file path="` + testLogFileName + `"/>937 </outputs>938 </outputs>939 </seelog>940 `941 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})942 testName = "Errors #26"943 testConfig = `944 <seelog type="sync">945 <outputs>946 <conn net="tcp" addr=":8888" reconnectonmsg="true1" />947 </outputs>948 </seelog>`949 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})950 testName = "Errors #27"951 testLogFileName = getTestFileName(testName, "")952 testConfig = `953 <seelog type="sync">954 <outputs>955 <rollingfile type="size" filename="` + testLogFileName + `" maxsize="100" maxrolls="5" archivetype="zip" archivepath="" />956 </outputs>957 </seelog>`958 parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil})959 testName = "Buffered writer same formatid override"960 testLogFileName = getTestFileName(testName, "")961 testConfig = `962 <seelog type="sync">963 <outputs>964 <buffered size="100500" flushperiod="100" formatid="testFormat">965 <rollingfile namemode="prefix" type="date" filename="` + testLogFileName + `" datepattern="2006-01-02T15:04:05Z07:00" formatid="testFormat"/>966 </buffered>967 </outputs>968 <formats>969 <format id="testFormat" format="%Level %Msg %File 123" />970 </formats>971 </seelog>`972 testExpected = new(configForParsing)973 testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)974 testExpected.Exceptions = nil975 testrollingFileWriterTime, _ = NewRollingFileWriterTime(testLogFileName, rollingArchiveNone, "", 0, "2006-01-02T15:04:05Z07:00", rollingNameModePrefix, false, false)976 testbufferedWriter, _ = NewBufferedWriter(testrollingFileWriterTime, 100500, 100)977 testFormat, _ = NewFormatter("%Level %Msg %File 123")978 formattedWriter, _ = NewFormattedWriter(testbufferedWriter, testFormat)979 testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{formattedWriter})980 testExpected.LogType = syncloggerTypeFromString981 testExpected.RootDispatcher = testHeadSplitter982 parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})983 }984 return parserTests985}986// Temporary solution: compare by string identity. Not the best solution in987// terms of performance, but a valid one in terms of comparison, because988// every seelog dispatcher/receiver must have a valid String() func989// that fully represents its internal parameters.990func configsAreEqual(conf1 *configForParsing, conf2 interface{}) bool {991 if conf1 == nil {992 return conf2 == nil993 }994 if conf2 == nil {995 return conf1 == nil996 }997 // configForParsing, ok := conf2 //.(*configForParsing)998 // if !ok {999 // return false1000 // }1001 return fmt.Sprintf("%v", conf1) == fmt.Sprintf("%v", conf2) //configForParsing)1002}1003func testLogFileFilter(fn string) bool {1004 return ".log" == filepath.Ext(fn)1005}1006func cleanupAfterCfgTest(t *testing.T) {1007 toDel, err := getDirFilePaths(".", testLogFileFilter, true)1008 if nil != err {1009 t.Fatal("Cannot list files in test directory!")1010 }1011 for _, p := range toDel {1012 err = tryRemoveFile(p)1013 if nil != err {1014 t.Errorf("cannot remove file %s in test directory: %s", p, err.Error())1015 }1016 }1017}1018func parseTest(test parserTest, t *testing.T) {1019 conf, err := configFromReaderWithConfig(strings.NewReader(test.config), test.parserConfig)1020 if /*err != nil &&*/ conf != nil && conf.RootDispatcher != nil {1021 defer func() {1022 if err = conf.RootDispatcher.Close(); err != nil {1023 t.Errorf("\n----ERROR while closing root dispatcher in %s test: %s", test.testName, err)1024 }1025 }()1026 }1027 if (err != nil) != test.errorExpected {1028 t.Errorf("\n----ERROR in %s:\nConfig: %s\n* Expected error:%t. Got error: %t\n",1029 test.testName, test.config, test.errorExpected, (err != nil))1030 if err != nil {1031 t.Logf("%s\n", err.Error())1032 }1033 return1034 }1035 if err == nil && !configsAreEqual(conf, test.expected) {1036 t.Errorf("\n----ERROR in %s:\nConfig: %s\n* Expected: %v. \n* Got: %v\n",1037 test.testName, test.config, test.expected, conf)1038 }1039}1040func TestParser(t *testing.T) {1041 defer cleanupAfterCfgTest(t)1042 for _, test := range getParserTests() {1043 parseTest(test, t)1044 }1045}...

Full Screen

Full Screen

image_proxy_test.go

Source:image_proxy_test.go Github

copy

Full Screen

...8 "testing"9 "github.com/gorilla/mux"10 "miniflux.app/config"11)12func TestProxyFilterWithHttpDefault(t *testing.T) {13 os.Clearenv()14 os.Setenv("PROXY_IMAGES", "http-only")15 var err error16 parser := config.NewParser()17 config.Opts, err = parser.ParseEnvironmentVariables()18 if err != nil {19 t.Fatalf(`Parsing failure: %v`, err)20 }21 r := mux.NewRouter()22 r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")23 input := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`24 output := ImageProxyRewriter(r, input)25 expected := `<p><img src="/proxy/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" alt="Test"/></p>`26 if expected != output {27 t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)28 }29}30func TestProxyFilterWithHttpsDefault(t *testing.T) {31 os.Clearenv()32 os.Setenv("PROXY_IMAGES", "http-only")33 var err error34 parser := config.NewParser()35 config.Opts, err = parser.ParseEnvironmentVariables()36 if err != nil {37 t.Fatalf(`Parsing failure: %v`, err)38 }39 r := mux.NewRouter()40 r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")41 input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`42 output := ImageProxyRewriter(r, input)43 expected := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`44 if expected != output {45 t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)46 }47}48func TestProxyFilterWithHttpNever(t *testing.T) {49 os.Clearenv()50 os.Setenv("PROXY_IMAGES", "none")51 var err error52 parser := config.NewParser()53 config.Opts, err = parser.ParseEnvironmentVariables()54 if err != nil {55 t.Fatalf(`Parsing failure: %v`, err)56 }57 r := mux.NewRouter()58 r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")59 input := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`60 output := ImageProxyRewriter(r, input)61 expected := input62 if expected != output {63 t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)64 }65}66func TestProxyFilterWithHttpsNever(t *testing.T) {67 os.Clearenv()68 os.Setenv("PROXY_IMAGES", "none")69 var err error70 parser := config.NewParser()71 config.Opts, err = parser.ParseEnvironmentVariables()72 if err != nil {73 t.Fatalf(`Parsing failure: %v`, err)74 }75 r := mux.NewRouter()76 r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")77 input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`78 output := ImageProxyRewriter(r, input)79 expected := input80 if expected != output {81 t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)82 }83}84func TestProxyFilterWithHttpAlways(t *testing.T) {85 os.Clearenv()86 os.Setenv("PROXY_IMAGES", "all")87 var err error88 parser := config.NewParser()89 config.Opts, err = parser.ParseEnvironmentVariables()90 if err != nil {91 t.Fatalf(`Parsing failure: %v`, err)92 }93 r := mux.NewRouter()94 r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")95 input := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`96 output := ImageProxyRewriter(r, input)97 expected := `<p><img src="/proxy/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" alt="Test"/></p>`98 if expected != output {99 t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)100 }101}102func TestProxyFilterWithHttpsAlways(t *testing.T) {103 os.Clearenv()104 os.Setenv("PROXY_IMAGES", "all")105 var err error106 parser := config.NewParser()107 config.Opts, err = parser.ParseEnvironmentVariables()108 if err != nil {109 t.Fatalf(`Parsing failure: %v`, err)110 }111 r := mux.NewRouter()112 r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")113 input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`114 output := ImageProxyRewriter(r, input)115 expected := `<p><img src="/proxy/aHR0cHM6Ly93ZWJzaXRlL2ZvbGRlci9pbWFnZS5wbmc=" alt="Test"/></p>`116 if expected != output {117 t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)118 }119}120func TestProxyFilterWithHttpInvalid(t *testing.T) {121 os.Clearenv()122 os.Setenv("PROXY_IMAGES", "invalid")123 var err error124 parser := config.NewParser()125 config.Opts, err = parser.ParseEnvironmentVariables()126 if err != nil {127 t.Fatalf(`Parsing failure: %v`, err)128 }129 r := mux.NewRouter()130 r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")131 input := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`132 output := ImageProxyRewriter(r, input)133 expected := `<p><img src="/proxy/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" alt="Test"/></p>`134 if expected != output {135 t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)136 }137}138func TestProxyFilterWithHttpsInvalid(t *testing.T) {139 os.Clearenv()140 os.Setenv("PROXY_IMAGES", "invalid")141 var err error142 parser := config.NewParser()143 config.Opts, err = parser.ParseEnvironmentVariables()144 if err != nil {145 t.Fatalf(`Parsing failure: %v`, err)146 }147 r := mux.NewRouter()148 r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")149 input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`150 output := ImageProxyRewriter(r, input)151 expected := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`152 if expected != output {153 t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)154 }155}156func TestProxyFilterWithSrcset(t *testing.T) {157 os.Clearenv()158 os.Setenv("PROXY_IMAGES", "all")159 var err error160 parser := config.NewParser()161 config.Opts, err = parser.ParseEnvironmentVariables()162 if err != nil {163 t.Fatalf(`Parsing failure: %v`, err)164 }165 r := mux.NewRouter()166 r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")167 input := `<p><img src="http://website/folder/image.png" srcset="http://website/folder/image2.png 656w, http://website/folder/image3.png 360w" alt="test"></p>`168 expected := `<p><img src="/proxy/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" srcset="/proxy/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlMi5wbmc= 656w, /proxy/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlMy5wbmc= 360w" alt="test"/></p>`169 output := ImageProxyRewriter(r, input)170 if expected != output {171 t.Errorf(`Not expected output: got %s`, output)172 }173}174func TestProxyFilterWithPictureSource(t *testing.T) {175 os.Clearenv()176 os.Setenv("PROXY_IMAGES", "all")177 var err error178 parser := config.NewParser()179 config.Opts, err = parser.ParseEnvironmentVariables()180 if err != nil {181 t.Fatalf(`Parsing failure: %v`, err)182 }183 r := mux.NewRouter()184 r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")185 input := `<picture><source srcset="http://website/folder/image2.png 656w, http://website/folder/image3.png 360w"></picture>`186 expected := `<picture><source srcset="/proxy/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlMi5wbmc= 656w, /proxy/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlMy5wbmc= 360w"/></picture>`187 output := ImageProxyRewriter(r, input)188 if expected != output {189 t.Errorf(`Not expected output: got %s`, output)190 }191}192func TestImageProxyWithImageDataURL(t *testing.T) {193 os.Clearenv()194 os.Setenv("PROXY_IMAGES", "all")195 var err error196 parser := config.NewParser()197 config.Opts, err = parser.ParseEnvironmentVariables()198 if err != nil {199 t.Fatalf(`Parsing failure: %v`, err)200 }201 r := mux.NewRouter()202 r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")203 input := `<img src="data:image/gif;base64,test">`204 expected := `<img src="data:image/gif;base64,test"/>`205 output := ImageProxyRewriter(r, input)206 if expected != output {207 t.Errorf(`Not expected output: got %s`, output)208 }209}210func TestImageProxyWithImageSourceDataURL(t *testing.T) {211 os.Clearenv()212 os.Setenv("PROXY_IMAGES", "all")213 var err error214 parser := config.NewParser()215 config.Opts, err = parser.ParseEnvironmentVariables()216 if err != nil {217 t.Fatalf(`Parsing failure: %v`, err)218 }219 r := mux.NewRouter()220 r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")221 input := `<picture><source srcset="data:image/gif;base64,test"/></picture>`222 expected := `<picture><source srcset="data:image/gif;base64,test"/></picture>`223 output := ImageProxyRewriter(r, input)224 if expected != output {...

Full Screen

Full Screen

parsers_test.go

Source:parsers_test.go Github

copy

Full Screen

...6 strValue string7 expectedValue interface{}8 expectedParser paramParser9}10func Test_Parsers(t *testing.T) {11 //ints12 checkParser(testDefinition{"1", 1, intParser{}}, t)13 checkParser(testDefinition{"-1", int64(-1), intParser{}}, t)14 checkParser(testDefinition{"1", uint64(1), intParser{}}, t)15 //floats16 checkParser(testDefinition{"1.0", float32(1.0), floatParser{}}, t)17 checkParser(testDefinition{"-1.0", float64(-1.0), floatParser{}}, t)18 //strings19 checkParser(testDefinition{"AB", "AB", stringParser{}}, t)20 checkParser(testDefinition{"AB", []byte{65, 66}, stringParser{}}, t)21 //bools22 checkParser(testDefinition{"true", true, boolParser{}}, t)23 checkParser(testDefinition{"0", false, boolParser{}}, t)24 //timeParser...

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Enter the path of the file")4 fmt.Scanln(&path)5 fmt.Println("Enter the number of tokens")6 fmt.Scanln(&n)7 s := scanner.NewScanner(path, n)8 s.Scan()9 p := parser.NewParser(s.GetTokens())10 p.Test()11}

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 p := parser.NewParser()4 p.Test()5 fmt.Println("Program executed successfully")6}

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 scanner := bufio.NewScanner(os.Stdin)4 fmt.Println("Enter the input string:")5 scanner.Scan()6 input := scanner.Text()7 parser := NewParser()8 output := parser.Parse(input)9 fmt.Println("Output:")10 fmt.Println(strings.Join(output, " "))11}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Gauge automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful