func TestUnwrap(t *testing.T) { tc := newTestUnwrapClient() vastutil.UpdateDefaultUnwrapClient(tc.Client) defer vastutil.RevertDefaultUnwrapClient() // The tests performs unwrapping of VAST XML starting from the entry point and is expected to // follow the trace until an error occurs or succeeds. tests := []struct { entry string // entry point for the VAST unwrapping test. trace []string // list of steps traces through. expectedError error // expected error in case error occurs. }{ {"malformed1", []string{}, &xml.SyntaxError{Line: 4, Msg: "unexpected EOF"}}, {"inline1", []string{}, nil}, {"wrapper3ads", []string{}, vastutil.ErrUnwrapWithMultipleAds}, {"wrapper4nouri", []string{}, vastutil.ErrWrapperMissingAdTagUri}, {"wrapper1", []string{"malformed1"}, &xml.SyntaxError{Line: 4, Msg: "unexpected EOF"}}, {"wrapper1", []string{"inline1"}, nil}, {"wrapper1", []string{"wrapper2", "inline1"}, nil}, } ctx := context.Background() for i, test := range tests { t.Logf("Testing %d starting from %s...", i, test.entry) tc.Init(test.trace) data, err := ioutil.ReadFile(testFilePath(test.entry)) if err != nil { t.Fatal(err) } else if err = tc.AddToServed(testFilePath(test.entry)); err != nil { t.Fatal(err) } vasts, err := vastutil.Unwrap(ctx, data) if err != nil && !reflect.DeepEqual(err, test.expectedError) { t.Log(reflect.TypeOf(err)) t.Errorf("Expecting error %v instead of %v.", test.expectedError, err) } else if err == nil { if len(tc.served) != len(test.trace)+1 { t.Errorf("Unwrapping VAST should hop %d times instead of %d times.", len(test.trace)+1, len(tc.served)) } else if len(vasts) != len(tc.served) { t.Errorf("Unwrapped VAST depth %d was different from served vast, %d.", len(vasts), len(tc.served)) } else if !reflect.DeepEqual(vasts, tc.served) { t.Error("Unwrapped VAST does not match with served VAST.", vasts, tc.served) } } } }
func TestUnwrapShouldRespectContext(t *testing.T) { // Given a context that is already finished. ctx, cancel := context.WithCancel(context.Background()) cancel() // When unwraps a wrapper VAST XML. data, err := ioutil.ReadFile(testFilePath("wrapper1")) if err != nil { t.Fatal(err) } _, err = vastutil.Unwrap(ctx, data) // Expect error to occur. if err != context.Canceled { t.Errorf("Error should have been %v instead of %v.", context.Canceled, err) } }