func TestGet(t *testing.T) { settings, err := Open("_examples/input/settings.yaml") if err != nil { t.Errorf("Test failed.") } test1 := "Hello World!" val1 := to.String(settings.Get("test_string")) if val1 != test1 { t.Errorf("Got %t expecting %t.", val1, test1) } val2 := int(to.Int64(settings.Get("non_defined_int"))) if val2 != 0 { t.Errorf("Test failed.") } test3 := "Third" val3 := settings.Get("test_map", "element_3", "test_sequence").([]interface{}) if val3[2] != test3 { t.Errorf("Got %t expecting %t.", val3[2], test3) } test5 := "Hello World!" val5 := to.String(settings.Get("test_string")) if test5 != val5 { t.Errorf("Got %t expecting %t.", test5, val5) } test6 := 1234 val6 := int(to.Int64(settings.Get("test_int"))) if test6 != val6 { t.Errorf("Got %t expecting %t.", test6, val6) } test7 := float64(1.2) val7 := to.Float64(settings.Get("test_float")) if test7 != val7 { t.Errorf("Got %t expecting %t.", test7, val7) } test8 := true val8 := to.Bool(settings.Get("test_bool")) if test8 != val8 { t.Errorf("Got %t expecting %t.", test8, val8) } }
func TestSet(t *testing.T) { settings, err := Open("_examples/input/settings.yaml") if err != nil { t.Errorf("Test failed.") } settings.Set("test_map", "element_3", "test_bool", true) test1 := true val1 := to.Bool(settings.Get("test_map", "element_3", "test_bool")) if val1 != test1 { t.Errorf("Got %t expecting %t.", val1, test1) } }
// Tests datatype conversions. func TestDataTypes(t *testing.T) { var res db.Result var items []db.Item sess, err := db.Open(wrapperName, settings) if err != nil { t.Fatalf(err.Error()) } defer sess.Close() dataTypes := sess.ExistentCollection("data_types") dataTypes.Truncate() ids, err := dataTypes.Append(testValues) if err != nil { t.Fatalf(err.Error()) } found, err := dataTypes.Count(db.Cond{"id": db.Id(ids[0])}) if err != nil { t.Fatalf(err.Error()) } if found == 0 { t.Errorf("Expecting an item.") } // Getting and reinserting (a db.Item). item, _ := dataTypes.Find() _, err = dataTypes.Append(item) if err == nil { t.Fatalf("Expecting duplicated-key error.") } delete(item, "id") _, err = dataTypes.Append(item) if err != nil { t.Fatalf(err.Error()) } // Testing date ranges items, err = dataTypes.FindAll(db.Cond{ "_date": time.Now(), }) if err != nil { t.Fatalf(err.Error()) } if len(items) > 0 { t.Fatalf("Expecting no results.") } items, err = dataTypes.FindAll(db.Cond{ "_date <=": time.Now(), }) if err != nil { t.Fatalf(err.Error()) } if len(items) != 2 { t.Fatalf("Expecting some results.") } // Testing struct sresults := []testValuesStruct{} res, err = dataTypes.Query() if err != nil { t.Fatalf(err.Error()) } err = res.All(&sresults) if err != nil { t.Fatalf(err.Error()) } // Testing struct equality for _, item := range sresults { if reflect.DeepEqual(item, testValues) == false { t.Errorf("Struct is different.") } } // Testing maps results, _ := dataTypes.FindAll() for _, item := range results { for key, _ := range item { switch key { // Signed integers. case "_int", "_int8", "_int16", "_int32", "_int64": if to.Int64(item[key]) != testValues.Int64 { t.Fatalf("Wrong datatype %v.", key) } // Unsigned integers. case "_uint", "_uint8", "_uint16", "_uint32", "_uint64": if to.Uint64(item[key]) != testValues.Uint64 { t.Fatalf("Wrong datatype %v.", key) } // Floating point. case "_float32": case "_float64": if to.Float64(item[key]) != testValues.Float64 { t.Fatalf("Wrong datatype %v.", key) } // Boolean case "_bool": if to.Bool(item[key]) != testValues.Bool { t.Fatalf("Wrong datatype %v.", key) } // String case "_string": if to.String(item[key]) != testValues.String { t.Fatalf("Wrong datatype %v.", key) } // Date case "_date": if to.Time(item[key]).Equal(testValues.Date) == false { t.Fatalf("Wrong datatype %v.", key) } } } } }
// downloadHandler provides a downloadable document given its ID. func downloadHandler(wri http.ResponseWriter, req *http.Request) { var err error var response getResponse if err = req.ParseForm(); err != nil { log.Printf("ParseForm: %q", err) return } wireFormat := to.Bool(req.Form.Get("wire")) direction := req.Form.Get("type") response.ID = uint(to.Int64(req.Form.Get("id"))) res := col.Find(response) res.Select( "id", "url", "method", "header", "request_header", "date_end", db.Raw{"hex(body) AS body"}, db.Raw{"hex(request_body) AS request_body"}, ) if err = res.One(&response.Response); err != nil { log.Printf("res.One: %q", err) return } var u *url.URL if u, err = url.Parse(response.URL); err != nil { log.Printf("url.Parse: %q", err) return } var body []byte basename := path.Base(u.Path) switch direction { case directionRequest: if body, err = hex.DecodeString(string(response.RequestBody)); err != nil { log.Printf("url.Parse: %q", err) return } if wireFormat { buf := bytes.NewBuffer(nil) buf.WriteString(fmt.Sprintf("%s %s HTTP/1.1\r\n", response.Method, u.RequestURI())) for k, vv := range response.RequestHeader.Header { for _, v := range vv { buf.WriteString(fmt.Sprintf("%s: %s\r\n", k, v)) } } buf.WriteString("\r\n") wri.Header().Set("Content-Disposition", `attachment; filename="`+u.Host+"-"+basename+`.bin"`) buf.Write(body) http.ServeContent(wri, req, "", response.DateEnd, bytes.NewReader(buf.Bytes())) } else { wri.Header().Set("Content-Disposition", `attachment; filename="`+basename+`"`) http.ServeContent(wri, req, basename, response.DateEnd, bytes.NewReader(body)) } default: if body, err = hex.DecodeString(string(response.Body)); err != nil { log.Printf("url.Parse: %q", err) return } if wireFormat { buf := bytes.NewBuffer(nil) for k, vv := range response.Header.Header { for _, v := range vv { buf.WriteString(fmt.Sprintf("%s: %s\r\n", k, v)) } } buf.WriteString("\r\n") wri.Header().Set("Content-Disposition", `attachment; filename="`+u.Host+"-"+basename+`.bin"`) buf.Write(body) http.ServeContent(wri, req, "", response.DateEnd, bytes.NewReader(buf.Bytes())) } else { wri.Header().Set("Content-Disposition", `attachment; filename="`+basename+`"`) http.ServeContent(wri, req, basename, response.DateEnd, bytes.NewReader(body)) } } return }
// downloadHandler provides a downloadable document given its ID. func downloadHandler(w http.ResponseWriter, r *http.Request) { var err error if err = r.ParseForm(); err != nil { log.Printf("ParseForm: %q", err) replyCode(w, http.StatusInternalServerError) return } wireFormat := to.Bool(r.Form.Get("wire")) direction := r.Form.Get("type") var response getResponse res := storage.Find(response.ID) res.Select( "id", "url", "method", "header", "request_header", "date_end", db.Raw("hex(body) AS body"), db.Raw("hex(request_body) AS request_body"), ) if err = res.One(&response.Response); err != nil { log.Printf("res.One: %q", err) replyCode(w, http.StatusInternalServerError) return } var u *url.URL if u, err = url.Parse(response.URL); err != nil { log.Printf("url.Parse: %q", err) replyCode(w, http.StatusInternalServerError) return } var body []byte basename := path.Base(u.Path) var headers http.Header if direction == directionRequest { if body, err = hex.DecodeString(string(response.RequestBody)); err != nil { log.Printf("url.Parse: %q", err) replyCode(w, http.StatusInternalServerError) return } headers = response.RequestHeader.Header } else { if body, err = hex.DecodeString(string(response.RequestBody)); err != nil { log.Printf("url.Parse: %q", err) replyCode(w, http.StatusInternalServerError) return } headers = response.Header.Header } if wireFormat { buf := bytes.NewBuffer(nil) for k, vv := range headers { for _, v := range vv { buf.WriteString(fmt.Sprintf("%s: %s\r\n", k, v)) } } buf.WriteString("\r\n") w.Header().Set("Content-Disposition", `attachment; filename="`+u.Host+"-"+basename+`.bin"`) buf.Write(body) http.ServeContent(w, r, "", response.DateEnd, bytes.NewReader(buf.Bytes())) return } w.Header().Set("Content-Disposition", `attachment; filename="`+basename+`"`) http.ServeContent(w, r, basename, response.DateEnd, bytes.NewReader(body)) }