feat(responses): implement responses and add a test
- implement rendering test for responses and requests - redo the API a bit to make responses and requests consistent
This commit is contained in:
parent
5cb5b5c4e9
commit
8a80b4e359
3 changed files with 257 additions and 104 deletions
|
@ -24,4 +24,3 @@ IsDelta="True"
|
|||
</updatecheck>
|
||||
</app>
|
||||
</response>
|
||||
|
||||
|
|
|
@ -8,11 +8,14 @@
|
|||
*/
|
||||
package omaha
|
||||
|
||||
import "encoding/xml"
|
||||
import (
|
||||
"encoding/xml"
|
||||
)
|
||||
|
||||
type Request struct {
|
||||
Os Os
|
||||
Apps []App `xml:"app"`
|
||||
XMLName xml.Name `xml:"request"`
|
||||
Os Os `xml:"os"`
|
||||
Apps []*App `xml:"app"`
|
||||
Protocol string `xml:"protocol,attr"`
|
||||
Version string `xml:"version,attr,omitempty"`
|
||||
IsMachine string `xml:"ismachine,attr,omitempty"`
|
||||
|
@ -24,24 +27,56 @@ type Request struct {
|
|||
UpdaterVersion string `xml:"updaterversion,attr,omitempty"`
|
||||
}
|
||||
|
||||
func NewRequest(os *Os, app *App) *Request {
|
||||
func NewRequest(version string, platform string, sp string, arch string) *Request {
|
||||
r := new(Request)
|
||||
r.Protocol = "3.0"
|
||||
r.AddApp(app)
|
||||
r.Os = *os
|
||||
r.Os.Version = version
|
||||
r.Os.Platform = platform
|
||||
r.Os.Sp = sp
|
||||
r.Os.Arch = arch
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Request) AddApp(a *App) {
|
||||
r.Apps = append(r.Apps, *a)
|
||||
func (r *Request) AddApp(id string, version string) *App {
|
||||
a := NewApp(id)
|
||||
a.Version = version
|
||||
r.Apps = append(r.Apps, a)
|
||||
return a
|
||||
}
|
||||
|
||||
/* Response
|
||||
*/
|
||||
type Response struct {
|
||||
XMLName xml.Name `xml:"response"`
|
||||
DayStart DayStart `xml:"daystart"`
|
||||
Apps []*App `xml:"app"`
|
||||
Protocol string `xml:"protocol,attr"`
|
||||
Server string `xml:"server,attr"`
|
||||
}
|
||||
|
||||
func NewResponse(server string) *Response {
|
||||
r := &Response{Server: server, Protocol: "3.0"}
|
||||
r.DayStart.ElapsedSeconds = "0"
|
||||
return r
|
||||
}
|
||||
|
||||
type DayStart struct {
|
||||
ElapsedSeconds string `xml:"elapsed_seconds,attr"`
|
||||
}
|
||||
|
||||
func (r *Response) AddApp(id string) *App {
|
||||
a := NewApp(id)
|
||||
r.Apps = append(r.Apps, a)
|
||||
return a
|
||||
}
|
||||
|
||||
// app element
|
||||
type App struct {
|
||||
XMLName xml.Name `xml:"app"`
|
||||
UpdateCheck *UpdateCheck `xml:"updatecheck"`
|
||||
Event *Event `xml:"event"`
|
||||
Ping *Ping `xml:"ping"`
|
||||
UpdateCheck *UpdateCheck `xml:"updatecheck"`
|
||||
Urls *Urls `xml:"urls"`
|
||||
Manifest *Manifest `xml:"manifest"`
|
||||
Event *Event `xml:"event"`
|
||||
Id string `xml:"appid,attr,omitempty"`
|
||||
Version string `xml:"version,attr,omitempty"`
|
||||
NextVersion string `xml:"nextversion,attr,omitempty"`
|
||||
|
@ -49,27 +84,49 @@ type App struct {
|
|||
Client string `xml:"client,attr,omitempty"`
|
||||
InstallAge string `xml:"installage,attr,omitempty"`
|
||||
FromTrack string `xml:"from_track,attr,omitempty"`
|
||||
Status string `xml:"status,attr,omitempty"`
|
||||
}
|
||||
|
||||
func NewApp(id string, version string) *App {
|
||||
a := new(App)
|
||||
a.Id = id
|
||||
a.Version = version
|
||||
func NewApp(id string) *App {
|
||||
a := &App{Id: id}
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *App) AddUpdateCheck() *UpdateCheck {
|
||||
a.UpdateCheck = new(UpdateCheck)
|
||||
return a.UpdateCheck
|
||||
}
|
||||
|
||||
func (a *App) AddPing() *Ping {
|
||||
a.Ping = new(Ping)
|
||||
return a.Ping
|
||||
}
|
||||
|
||||
func (a *App) AddUrl(codebase string) *Url {
|
||||
if a.Urls == nil {
|
||||
a.Urls = new(Urls)
|
||||
}
|
||||
u := new(Url)
|
||||
u.CodeBase = codebase
|
||||
a.Urls.Urls = append(a.Urls.Urls, *u)
|
||||
return u
|
||||
}
|
||||
|
||||
func (a *App) AddManifest(version string) *Manifest {
|
||||
a.Manifest = &Manifest{Version: version}
|
||||
return a.Manifest
|
||||
}
|
||||
|
||||
type UpdateCheck struct {
|
||||
XMLName xml.Name `xml:"updatecheck"`
|
||||
TargetVersionPrefix string `xml:"targetversionprefix,attr,omitempty"`
|
||||
}
|
||||
|
||||
func (a *App) AddUpdateCheck() {
|
||||
a.UpdateCheck = new(UpdateCheck)
|
||||
Status string `xml:"status,attr,omitempty"`
|
||||
}
|
||||
|
||||
type Ping struct {
|
||||
XMLName xml.Name `xml:"ping"`
|
||||
LastReportDays string `xml:"r,attr,omitempty"`
|
||||
Status string `xml:"status,attr,omitempty"`
|
||||
}
|
||||
|
||||
type Os struct {
|
||||
|
@ -82,16 +139,13 @@ type Os struct {
|
|||
|
||||
func NewOs(platform string, version string, sp string, arch string) *Os {
|
||||
o := new(Os)
|
||||
o.Platform = platform
|
||||
o.Version = version
|
||||
o.Platform = platform
|
||||
o.Sp = sp
|
||||
o.Arch = arch
|
||||
return o
|
||||
}
|
||||
|
||||
func (a *App) AddPing() {
|
||||
}
|
||||
|
||||
type Event struct {
|
||||
XMLName xml.Name `xml:"event"`
|
||||
Type string `xml:"eventtype,attr,omitempty"`
|
||||
|
@ -99,6 +153,62 @@ type Event struct {
|
|||
PreviousVersion string `xml:"previousversion,attr,omitempty"`
|
||||
}
|
||||
|
||||
type Urls struct {
|
||||
XMLName xml.Name `xml:"urls"`
|
||||
Urls []Url `xml:"url"`
|
||||
}
|
||||
|
||||
type Url struct {
|
||||
XMLName xml.Name `xml:"url"`
|
||||
CodeBase string `xml:"codebase,attr"`
|
||||
}
|
||||
|
||||
type Manifest struct {
|
||||
XMLName xml.Name `xml:"manifest"`
|
||||
Packages Packages `xml:"packages"`
|
||||
Actions Actions `xml:"actions"`
|
||||
Version string `xml:"version,attr"`
|
||||
}
|
||||
|
||||
type Packages struct {
|
||||
XMLName xml.Name `xml:"packages"`
|
||||
Packages []Package `xml:"package"`
|
||||
}
|
||||
|
||||
type Package struct {
|
||||
XMLName xml.Name `xml:"package"`
|
||||
Hash string `xml:"hash,attr"`
|
||||
Name string `xml:"name,attr"`
|
||||
Size string `xml:"size,attr"`
|
||||
Required bool `xml:"required,attr"`
|
||||
}
|
||||
|
||||
func (m *Manifest) AddPackage(hash string, name string, size string, required bool) *Package {
|
||||
p := &Package{Hash: hash, Name: name, Size: size, Required: required}
|
||||
m.Packages.Packages = append(m.Packages.Packages, *p)
|
||||
return p
|
||||
}
|
||||
|
||||
type Actions struct {
|
||||
XMLName xml.Name `xml:"actions"`
|
||||
Actions []Action `xml:"action"`
|
||||
}
|
||||
|
||||
type Action struct {
|
||||
XMLName xml.Name `xml:"action"`
|
||||
Event string `xml:"event,attr"`
|
||||
ChromeOSVersion string `xml:"ChromeOSVersion,attr"`
|
||||
sha256 string `xml:"sha256,attr"`
|
||||
NeedsAdmin bool `xml:"needsadmin,attr"`
|
||||
IsDelta bool `xml:"IsDelta,attr"`
|
||||
}
|
||||
|
||||
func (m *Manifest) AddAction(event string) *Action {
|
||||
a := &Action{Event: event}
|
||||
m.Actions.Actions = append(m.Actions.Actions, *a)
|
||||
return a
|
||||
}
|
||||
|
||||
var EventTypes = map[int]string{
|
||||
0: "unknown",
|
||||
1: "download complete",
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package omaha
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"fmt"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOmahaRequestUpdateCheck(t *testing.T) {
|
||||
|
@ -45,14 +45,58 @@ func TestOmahaRequestUpdateCheck(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func ExampleOmaha_NewResponse() {
|
||||
response := NewResponse("unit-test")
|
||||
app := response.AddApp("{52F1B9BC-D31A-4D86-9276-CBC256AADF9A}")
|
||||
app.Status = "ok"
|
||||
p := app.AddPing()
|
||||
p.Status = "ok"
|
||||
u := app.AddUpdateCheck()
|
||||
u.Status = "ok"
|
||||
app.AddUrl("http://localhost/updates")
|
||||
m := app.AddManifest("9999.0.0")
|
||||
m.AddPackage("+LXvjiaPkeYDLHoNKlf9qbJwvnk=", "update.gz", "67546213", true)
|
||||
a := m.AddAction("postinstall")
|
||||
a.ChromeOSVersion = "9999.0.0"
|
||||
a.sha256 = "0VAlQW3RE99SGtSB5R4m08antAHO8XDoBMKDyxQT/Mg="
|
||||
a.NeedsAdmin = false
|
||||
a.IsDelta = true
|
||||
|
||||
if raw, err := xml.MarshalIndent(response, "", " "); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
} else {
|
||||
fmt.Printf("%s%s\n", xml.Header, raw)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// <?xml version="1.0" encoding="UTF-8"?>
|
||||
//
|
||||
// <response protocol="3.0" server="unit-test">
|
||||
// <daystart elapsed_seconds="0"></daystart>
|
||||
// <app appid="{52F1B9BC-D31A-4D86-9276-CBC256AADF9A}" status="ok">
|
||||
// <ping status="ok"></ping>
|
||||
// <updatecheck status="ok"></updatecheck>
|
||||
// <urls>
|
||||
// <url codebase="http://localhost/updates"></url>
|
||||
// </urls>
|
||||
// <manifest version="9999.0.0">
|
||||
// <packages>
|
||||
// <package hash="+LXvjiaPkeYDLHoNKlf9qbJwvnk=" name="update.gz" size="67546213" required="true"></package>
|
||||
// </packages>
|
||||
// <actions>
|
||||
// <action event="postinstall" ChromeOSVersion="" needsadmin="false" IsDelta="false"></action>
|
||||
// </actions>
|
||||
// </manifest>
|
||||
// </app>
|
||||
// </response>
|
||||
}
|
||||
|
||||
func ExampleOmaha_NewRequest() {
|
||||
os := NewOs("linux", "3.0", "", "x64")
|
||||
|
||||
app := NewApp("{27BD862E-8AE8-4886-A055-F7F1A6460627}", "1.0.0.0")
|
||||
request := NewRequest("Indy", "Chrome OS", "ForcedUpdate_x86_64", "")
|
||||
app := request.AddApp("{27BD862E-8AE8-4886-A055-F7F1A6460627}", "1.0.0.0")
|
||||
app.AddUpdateCheck()
|
||||
|
||||
request := NewRequest(os, app)
|
||||
|
||||
if raw, err := xml.MarshalIndent(request, "", " "); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
|
@ -63,10 +107,10 @@ func ExampleOmaha_NewRequest() {
|
|||
// Output:
|
||||
// <?xml version="1.0" encoding="UTF-8"?>
|
||||
//
|
||||
// <Request protocol="3.0">
|
||||
// <os platform="linux" version="3.0" arch="x64"></os>
|
||||
// <request protocol="3.0">
|
||||
// <os platform="Chrome OS" version="Indy" sp="ForcedUpdate_x86_64"></os>
|
||||
// <app appid="{27BD862E-8AE8-4886-A055-F7F1A6460627}" version="1.0.0.0">
|
||||
// <updatecheck></updatecheck>
|
||||
// </app>
|
||||
// </Request>
|
||||
// </request>
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue