func TestUaAuth(t *testing.T) { uaauth := UserAgentAuthentication{ UserAgent: "user-agent", UserAgentPassword: "******", } header1 := uaauth.header("request-id", "session-id", "rets-version") testutils.Assert(t, "Digest 052c1af08431d3cc9717a37f9d6de169" == header1, "bad auth") header2 := uaauth.header("", "", "rets-version") testutils.Assert(t, "Digest 73cc7ccfe417292b1155c5ccee7fbdab" == header2, "bad auth") }
func TestFieldTransfer(t *testing.T) { type Tester struct { Bob string foo string Baz string } test := &Tester{} FieldTransfer(map[string]string{ "Bob": "foo", "Foo": "bar", "baz": "blah", }).To(test) testutils.Equals(t, "foo", test.Bob) testutils.Assert(t, "" == test.foo, "shouldnt be able to set lower case fields") testutils.Assert(t, "blah" == test.Baz, "should be case insensitive") }
func TestSearchCompactParseCompact(t *testing.T) { body := ioutil.NopCloser(strings.NewReader(compactDecoded)) cr, err := NewCompactSearchResult(body) testutils.Ok(t, err) testutils.Assert(t, StatusOK == cr.Response.Code, "bad code") testutils.Assert(t, "V2.7.0 2315: Success" == cr.Response.Text, "bad text") testutils.Assert(t, 10 == int(cr.Count), "bad count") testutils.Assert(t, 6 == len(cr.Columns), "bad header count") testutils.Assert(t, "A,B,C,D,E,F" == strings.Join(cr.Columns, ","), "bad headers") counter := 0 maxRows, err := cr.ForEach(func(row Row, err error) error { if strings.Join(row, ",") != "1,2,3,4,,6" { t.Errorf("bad row %d: %s", counter, row) } if err != nil { t.Errorf("error parsing body at row %d: %s", counter, err.Error()) } counter = counter + 1 return nil }) testutils.Ok(t, err) testutils.Assert(t, 8 == counter, "bad count") testutils.Assert(t, maxRows, "bad max rows") }
func TestParseCompactData(t *testing.T) { body := ioutil.NopCloser(strings.NewReader(compact)) decoder := DefaultXMLDecoder(body, false) token, err := decoder.Token() testutils.Ok(t, err) start, ok := token.(xml.StartElement) testutils.Assert(t, ok, "should be a start element") testutils.Equals(t, "METADATA-ELEMENT", start.Name.Local) cm, err := NewCompactData(start, decoder, " ") testutils.Ok(t, err) testutils.Equals(t, "METADATA-ELEMENT", cm.Element) testutils.Equals(t, "Dog", cm.Attr["Cat"]) testutils.Equals(t, 2, len(cm.CompactRows)) testutils.Equals(t, 2, len(cm.Columns())) }
func TestCompactEntry(t *testing.T) { body := ioutil.NopCloser(strings.NewReader(compact)) decoder := DefaultXMLDecoder(body, false) token, err := decoder.Token() testutils.Ok(t, err) start, ok := token.(xml.StartElement) testutils.Assert(t, ok, "should be a start element") cm, err := NewCompactData(start, decoder, " ") testutils.Ok(t, err) type Test struct { ResourceID, Standardname string } row1 := Test{} maps := cm.Entries() maps[0].SetFields(&row1) testutils.Equals(t, "ActiveAgent", row1.ResourceID) testutils.Equals(t, "ActiveAgent", row1.Standardname) }
func TestSearchCompactParseSearchQuit(t *testing.T) { noEnd := strings.Split(compactDecoded, "<MAXROWS/>")[0] body := ioutil.NopCloser(strings.NewReader(noEnd)) cr, err := NewCompactSearchResult(body) testutils.Ok(t, err) rowsFound := 0 cr.ForEach(func(data Row, err error) error { if err != nil { testutils.Assert(t, strings.Contains(err.Error(), "EOF"), "found something not eof") return err } testutils.Equals(t, "1,2,3,4,,6", strings.Join(data, ",")) rowsFound++ return nil }) testutils.Equals(t, 8, rowsFound) }
func TestGetObjects(t *testing.T) { headers := http.Header{} headers.Add("Content-Type", contentType) response := GetObjectResponse{ response: &http.Response{ Header: headers, Body: ioutil.NopCloser(strings.NewReader(multipartBody)), }, } defer response.Close() var objects []*Object response.ForEach(func(o *Object, err error) error { testutils.Ok(t, err) objects = append(objects, o) return nil }) o1 := objects[0] testutils.Equals(t, true, o1.Preferred) testutils.Equals(t, "image/jpeg", o1.ContentType) testutils.Equals(t, "123456", o1.ContentID) testutils.Equals(t, 1, o1.ObjectID) testutils.Equals(t, "<binary data 1>", string(o1.Blob)) testutils.Equals(t, "123456", o1.ObjectData["ListingKey"]) testutils.Equals(t, "2013-05-01T12:34:34.8-0500", o1.ObjectData["ListDate"]) o2 := objects[1] testutils.Equals(t, 2, o2.ObjectID) testutils.Equals(t, "1a234234234", o2.UID) o3 := objects[2] testutils.Equals(t, 3, o3.ObjectID) testutils.Equals(t, "Outhouse", o3.Description) testutils.Equals(t, "The urinal", o3.SubDescription) o4 := objects[3] testutils.Equals(t, true, o4.RetsError) testutils.Equals(t, "text/xml", o4.ContentType) testutils.Equals(t, "There is no object with that Object-ID", o4.RetsMessage.Text) testutils.Equals(t, StatusObjectNotFound, o4.RetsMessage.Code) o5 := objects[4] testutils.Equals(t, "http://www.simpleboundary.com/image-5.jpg", o5.Location) testutils.Equals(t, "image/jpeg", o5.ContentType) testutils.Equals(t, "123457", o5.ContentID) testutils.Equals(t, 5, o5.ObjectID) testutils.Equals(t, "", string(o5.Blob)) o6 := objects[5] testutils.Equals(t, "http://www.simpleboundary.com/image-6.jpg", o6.Location) testutils.Equals(t, "image/jpeg", o6.ContentType) testutils.Equals(t, "123457", o6.ContentID) testutils.Equals(t, 6, o6.ObjectID) testutils.Equals(t, "<binary data 6>", string(o6.Blob)) testutils.Assert(t, o6.RetsMessage == nil, "should not be the zerod object") o7 := objects[6] testutils.Equals(t, "http://www.simpleboundary.com/image-7.jpg", o7.Location) testutils.Equals(t, "image/jpeg", o7.ContentType) testutils.Equals(t, "123457", o7.ContentID) testutils.Equals(t, 7, o7.ObjectID) testutils.Equals(t, "", string(o7.Blob)) testutils.Equals(t, "Found it!", o7.RetsMessage.Text) }