func TestNewSubListRqPl(t *testing.T) {
	expectedReq := "<iq type=\"get\" id=\"subman1\" to=\"pubsub.shakespeare.lit\" > " +
		"<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> " +
		"<subscriptions node=\"princely_musings\"></subscriptions> </pubsub> </iq>"
	subR, err := stanza.NewSubListRqPl("pubsub.shakespeare.lit", "princely_musings")
	if err != nil {
		t.Fatalf("failed to create a sub list request: %v", err)
	}
	subR.Id = "subman1"
	if _, e := checkMarshalling(t, subR); e != nil {
		t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
	}
	pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
	if !ok {
		t.Fatalf("payload is not a pubsub in namespace owner !")
	}
	subs, ok := pubsub.OwnerUseCase.(*stanza.SubscriptionsOwner)
	if !ok {
		t.Fatalf("pubsub doesn not contain a subscriptions node !")
	}
	if subs.Node != "princely_musings" {
		t.Fatalf("subs node attribute should be princely_musings. Found %s", subs.Node)
	}
	data, err := xml.Marshal(subR)
	if err := compareMarshal(expectedReq, string(data)); err != nil {
		t.Fatalf(err.Error())
	}
}
********************************************
* 8.8.1 Retrieve Subscriptions List
********************************************
package stanza_test
import (
	"encoding/xml"
	"errors"
	"gosrc.io/xmpp/stanza"
	"testing"
)
func TestNewConfigureNode(t *testing.T) {
	expectedReq := "<iq type=\"get\" id=\"config1\" to=\"pubsub.shakespeare.lit\" > " +
		"<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> <configure node=\"princely_musings\"></configure> " +
		"</pubsub> </iq>"
	subR, err := stanza.NewConfigureNode("pubsub.shakespeare.lit", "princely_musings")
	if err != nil {
		t.Fatalf("failed to create a configure node request: %v", err)
	}
	subR.Id = "config1"
	if _, e := checkMarshalling(t, subR); e != nil {
		t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
	}
	pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
	if !ok {
		t.Fatalf("payload is not a pubsub !")
	}
	if pubsub.OwnerUseCase == nil {
		t.Fatalf("owner use case is nil")
	}
	ownrUsecase, ok := pubsub.OwnerUseCase.(*stanza.ConfigureOwner)
	if !ok {
		t.Fatalf("owner use case is not a configure tag")
	}
	if ownrUsecase.Node == "" {
		t.Fatalf("could not parse node from config tag")
	}
	data, err := xml.Marshal(subR)
	if err := compareMarshal(expectedReq, string(data)); err != nil {
		t.Fatalf(err.Error())
	}
}
func TestNewConfigureNodeResp(t *testing.T) {
	response := `
	<iq from="pubsub.shakespeare.lit" id="config1" to="hamlet@denmark.lit/elsinore" type="result">
  <pubsub xmlns="http://jabber.org/protocol/pubsub#owner">
    <configure node="princely_musings">
      <x type="form" xmlns="jabber:x:data">
        <field type="hidden" var="FORM_TYPE">
          <value>http: 
        </field>
        <field label="Purge all items when the relevant publisher goes offline?" type="boolean" var="pubsub#purge_offline">
          <value>0</value>
        </field>
        <field label="Max Payload size in bytes" type="text-single" var="pubsub#max_payload_size">
          <value>1028</value>
        </field>
        <field label="When to send the last published item" type="list-single" var="pubsub#send_last_published_item">
          <option label="Never">
            <value>never</value>
          </option>
          <option label="When a new subscription is processed">
            <value>on_sub</value>
          </option>
          <option label="When a new subscription is processed and whenever a subscriber comes online">
            <value>on_sub_and_presence</value>
          </option>
          <value>never</value>
        </field>
        <field label="Deliver event notifications only to available users" type="boolean" var="pubsub#presence_based_delivery">
          <value>0</value>
        </field>
        <field label="Specify the delivery style for event notifications" type="list-single" var="pubsub#notification_type">
          <option>
            <value>normal</value>
          </option>
          <option>
            <value>headline</value>
          </option>
          <value>headline</value>
        </field>
        <field label="Specify the type of payload data to be provided at this node" type="text-single" var="pubsub#type">
          <value>http: 
        </field>
        <field label="Payload XSLT" type="text-single" var="pubsub#dataform_xslt"/>
      </x>
    </configure>
  </pubsub>
</iq>
`
	pubsub, err := getPubSubOwnerPayload(response)
	if err != nil {
		t.Fatalf(err.Error())
	}
	if pubsub.OwnerUseCase == nil {
		t.Fatalf("owner use case is nil")
	}
	ownrUsecase, ok := pubsub.OwnerUseCase.(*stanza.ConfigureOwner)
	if !ok {
		t.Fatalf("owner use case is not a configure tag")
	}
	if ownrUsecase.Form == nil {
		t.Fatalf("form is nil in the parsed config tag")
	}
	if len(ownrUsecase.Form.Fields) != 8 {
		t.Fatalf("one or more fields in the response form could not be parsed correctly")
	}
}
func TestNewRequestDefaultConfig(t *testing.T) {
	expectedReq := "<iq type=\"get\" id=\"def1\" to=\"pubsub.shakespeare.lit\"> " +
		"<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> <default></default> </pubsub> </iq>"
	subR, err := stanza.NewRequestDefaultConfig("pubsub.shakespeare.lit")
	if err != nil {
		t.Fatalf("failed to create a default config request: %v", err)
	}
	subR.Id = "def1"
	if _, e := checkMarshalling(t, subR); e != nil {
		t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
	}
	pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
	if !ok {
		t.Fatalf("payload is not a pubsub !")
	}
	if pubsub.OwnerUseCase == nil {
		t.Fatalf("owner use case is nil")
	}
	_, ok = pubsub.OwnerUseCase.(*stanza.DefaultOwner)
	if !ok {
		t.Fatalf("owner use case is not a default tag")
	}
	data, err := xml.Marshal(subR)
	if err := compareMarshal(expectedReq, string(data)); err != nil {
		t.Fatalf(err.Error())
	}
}
func TestNewRequestDefaultConfigResp(t *testing.T) {
	response := `
	<iq from="pubsub.shakespeare.lit" id="config1" to="hamlet@denmark.lit/elsinore" type="result">
 <pubsub xmlns="http://jabber.org/protocol/pubsub#owner">
   <configure node="princely_musings">
     <x type="form" xmlns="jabber:x:data">
       <field type="hidden" var="FORM_TYPE">
         <value>http: 
       </field>
       <field label="Purge all items when the relevant publisher goes offline?" type="boolean" var="pubsub#purge_offline">
         <value>0</value>
       </field>
       <field label="Max Payload size in bytes" type="text-single" var="pubsub#max_payload_size">
         <value>1028</value>
       </field>
       <field label="When to send the last published item" type="list-single" var="pubsub#send_last_published_item">
         <option label="Never">
           <value>never</value>
         </option>
         <option label="When a new subscription is processed">
           <value>on_sub</value>
         </option>
         <option label="When a new subscription is processed and whenever a subscriber comes online">
           <value>on_sub_and_presence</value>
         </option>
         <value>never</value>
       </field>
       <field label="Deliver event notifications only to available users" type="boolean" var="pubsub#presence_based_delivery">
         <value>0</value>
       </field>
       <field label="Specify the delivery style for event notifications" type="list-single" var="pubsub#notification_type">
         <option>
           <value>normal</value>
         </option>
         <option>
           <value>headline</value>
         </option>
         <value>headline</value>
       </field>
       <field label="Specify the type of payload data to be provided at this node" type="text-single" var="pubsub#type">
         <value>http: 
       </field>
       <field label="Payload XSLT" type="text-single" var="pubsub#dataform_xslt"/>
     </x>
   </configure>
 </pubsub>
</iq>
`
	pubsub, err := getPubSubOwnerPayload(response)
	if err != nil {
		t.Fatalf(err.Error())
	}
	if pubsub.OwnerUseCase == nil {
		t.Fatalf("owner use case is nil")
	}
	ownrUsecase, ok := pubsub.OwnerUseCase.(*stanza.ConfigureOwner)
	if !ok {
		t.Fatalf("owner use case is not a configure tag")
	}
	if ownrUsecase.Form == nil {
		t.Fatalf("form is nil in the parsed config tag")
	}
	if len(ownrUsecase.Form.Fields) != 8 {
		t.Fatalf("one or more fields in the response form could not be parsed correctly")
	}
}
func TestNewDelNode(t *testing.T) {
	expectedReq := "<iq type=\"set\" id=\"delete1\" to=\"pubsub.shakespeare.lit\" >" +
		" <pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> " +
		"<delete node=\"princely_musings\"></delete> </pubsub> </iq>"
	subR, err := stanza.NewDelNode("pubsub.shakespeare.lit", "princely_musings")
	if err != nil {
		t.Fatalf("failed to create a node delete request: %v", err)
	}
	subR.Id = "delete1"
	if _, e := checkMarshalling(t, subR); e != nil {
		t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
	}
	pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
	if !ok {
		t.Fatalf("payload is not a pubsub !")
	}
	if pubsub.OwnerUseCase == nil {
		t.Fatalf("owner use case is nil")
	}
	_, ok = pubsub.OwnerUseCase.(*stanza.DeleteOwner)
	if !ok {
		t.Fatalf("owner use case is not a delete tag")
	}
	data, err := xml.Marshal(subR)
	if err := compareMarshal(expectedReq, string(data)); err != nil {
		t.Fatalf(err.Error())
	}
}
func TestNewDelNodeResp(t *testing.T) {
	response := `
	<iq id="delete1" to="pubsub.shakespeare.lit" type="set">
    <pubsub xmlns="http://jabber.org/protocol/pubsub#owner">
        <delete node="princely_musings">
            <redirect uri="xmpp:hamlet@denmark.lit"/>
        </delete>
    </pubsub>
</iq>
`
	pubsub, err := getPubSubOwnerPayload(response)
	if err != nil {
		t.Fatalf(err.Error())
	}
	if pubsub.OwnerUseCase == nil {
		t.Fatalf("owner use case is nil")
	}
	ownrUsecase, ok := pubsub.OwnerUseCase.(*stanza.DeleteOwner)
	if !ok {
		t.Fatalf("owner use case is not a configure tag")
	}
	if ownrUsecase.RedirectOwner == nil {
		t.Fatalf("redirect is nil in the delete tag")
	}
	if ownrUsecase.RedirectOwner.URI == "" {
		t.Fatalf("could not parse redirect uri")
	}
}
func TestNewPurgeAllItems(t *testing.T) {
	expectedReq := "<iq type=\"set\" id=\"purge1\" to=\"pubsub.shakespeare.lit\"> " +
		"<pubsub xmlns=\"http://jabber.org/protocol/pubsub#owner\"> " +
		"<purge node=\"princely_musings\"></purge> </pubsub> </iq>"
	subR, err := stanza.NewPurgeAllItems("pubsub.shakespeare.lit", "princely_musings")
	if err != nil {
		t.Fatalf("failed to create a purge all items request: %v", err)
	}
	subR.Id = "purge1"
	if _, e := checkMarshalling(t, subR); e != nil {
		t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
	}
	pubsub, ok := subR.Payload.(*stanza.PubSubOwner)
	if !ok {
		t.Fatalf("payload is not a pubsub !")
	}
	if pubsub.OwnerUseCase == nil {
		t.Fatalf("owner use case is nil")
	}
	purge, ok := pubsub.OwnerUseCase.(*stanza.PurgeOwner)
	if !ok {
		t.Fatalf("owner use case is not a delete tag")
	}
	if purge.Node == "" {
		t.Fatalf("could not parse purge targer node")
	}
	data, err := xml.Marshal(subR)
	if err := compareMarshal(expectedReq, string(data)); err != nil {
		t.Fatalf(err.Error())
	}
}
func TestNewApproveSubRequest(t *testing.T) {
	expectedReq := "<message id=\"approve1\" to=\"pubsub.shakespeare.lit\"> " +
		"<x xmlns=\"jabber:x:data\" type=\"submit\"> <field var=\"FORM_TYPE\" type=\"hidden\"> " +
		"<value>http://jabber.org/protocol/pubsub#subscribe_authorization</value> </field> <field var=\"pubsub#subid\">" +
		" <value>123-abc</value> </field> <field var=\"pubsub#node\"> <value>princely_musings</value> </field> " +
		"<field var=\"pubsub#subscriber_jid\"> <value>horatio@denmark.lit</value> </field> <field var=\"pubsub#allow\"> " +
		"<value>true</value> </field> </x> </message>"
	apprForm := &stanza.Form{
		Type: stanza.FormTypeSubmit,
		Fields: []*stanza.Field{
			{Var: "FORM_TYPE", Type: stanza.FieldTypeHidden, ValuesList: []string{"http://jabber.org/protocol/pubsub#subscribe_authorization"}},
			{Var: "pubsub#subid", ValuesList: []string{"123-abc"}},
			{Var: "pubsub#node", ValuesList: []string{"princely_musings"}},
			{Var: "pubsub#subscriber_jid", ValuesList: []string{"horatio@denmark.lit"}},
			{Var: "pubsub#allow", ValuesList: []string{"true"}},
		},
	}
	subR, err := stanza.NewApproveSubRequest("pubsub.shakespeare.lit", "approve1", apprForm)
	if err != nil {
		t.Fatalf("failed to create a sub approval request: %v", err)
	}
	subR.Id = "approve1"
	frm, ok := subR.Extensions[0].(*stanza.Form)
	if !ok {
		t.Fatalf("extension is not a from !")
	}
	var allowField *stanza.Field
	for _, f := range frm.Fields {
		if f.Var == "pubsub#allow" {
			allowField = f
		}
	}
	if allowField == nil || allowField.ValuesList[0] != "true" {
		t.Fatalf("could not correctly parse the allow field in the response from")
	}
	data, err := xml.Marshal(subR)
	if err := compareMarshal(expectedReq, string(data)); err != nil {
		t.Fatalf(err.Error())
	}
}
func TestNewGetPendingSubRequests(t *testing.T) {
	expectedReq := "<iq type=\"set\" id=\"pending1\" to=\"pubsub.shakespeare.lit\" > " +
		"<command xmlns=\"http://jabber.org/protocol/commands\"  action=\"execute\" node=\"http://jabber.org/protocol/pubsub#get-pending\" >" +
		"</command> </iq>"
	subR, err := stanza.NewGetPendingSubRequests("pubsub.shakespeare.lit")
	if err != nil {
		t.Fatalf("failed to create a get pending subs request: %v", err)
	}
	subR.Id = "pending1"
	if _, e := checkMarshalling(t, subR); e != nil {
		t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
	}
	command, ok := subR.Payload.(*stanza.Command)
	if !ok {
		t.Fatalf("payload is not a command !")
	}
	if command.Action != stanza.CommandActionExecute {
		t.Fatalf("command should be execute !")
	}
	if command.Node != "http://jabber.org/protocol/pubsub#get-pending" {
		t.Fatalf("command node should be http://jabber.org/protocol/pubsub#get-pending !")
	}
	data, err := xml.Marshal(subR)
	if err := compareMarshal(expectedReq, string(data)); err != nil {
		t.Fatalf(err.Error())
	}
}
func TestNewGetPendingSubRequestsResp(t *testing.T) {
	response := `
	<iq from="pubsub.shakespeare.lit" id="pending1" to="hamlet@denmark.lit/elsinore" type="result">
  <command action="execute" node="http://jabber.org/protocol/pubsub#get-pending" sessionid="pubsub-get-pending:20031021T150901Z-600" status="executing" xmlns="http://jabber.org/protocol/commands">
    <x type="form" xmlns="jabber:x:data">
      <field type="hidden" var="FORM_TYPE">
        <value>http: 
      </field>
      <field type="list-single" var="pubsub#node">
        <option>
          <value>princely_musings</value>
        </option>
        <option>
          <value>news_from_elsinore</value>
        </option>
      </field>
    </x>
  </command>
</iq>
`
	var respIQ stanza.IQ
	err := xml.Unmarshal([]byte(response), &respIQ)
	if err != nil {
		t.Fatalf("could not parse iq")
	}
	_, ok := respIQ.Payload.(*stanza.Command)
	if !ok {
		t.Fatal("this iq payload is not a command")
	}
	fMap, err := respIQ.GetFormFields()
	if err != nil || len(fMap) != 2 {
		t.Fatal("could not parse command form fields")
	}
}
func TestNewApprovePendingSubRequest(t *testing.T) {
	expectedReq := "<iq type=\"set\" id=\"pending2\" to=\"pubsub.shakespeare.lit\"> " +
		"<command xmlns=\"http://jabber.org/protocol/commands\" action=\"execute\"" +
		"node=\"http://jabber.org/protocol/pubsub#get-pending\"sessionid=\"pubsub-get-pending:20031021T150901Z-600\"> " +
		"<x xmlns=\"jabber:x:data\" type=\"submit\"> <field xmlns=\"jabber:x:data\" var=\"pubsub#node\"> " +
		"<value xmlns=\"jabber:x:data\">princely_musings</value> </field> </x> </command> </iq>"
	subR, err := stanza.NewApprovePendingSubRequest("pubsub.shakespeare.lit",
		"pubsub-get-pending:20031021T150901Z-600",
		"princely_musings")
	if err != nil {
		t.Fatalf("failed to create a approve pending sub request: %v", err)
	}
	subR.Id = "pending2"
	if _, e := checkMarshalling(t, subR); e != nil {
		t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
	}
	command, ok := subR.Payload.(*stanza.Command)
	if !ok {
		t.Fatalf("payload is not a command !")
	}
	if command.Action != stanza.CommandActionExecute {
		t.Fatalf("command should be execute !")
	}
	data, err := xml.Marshal(subR)
	if err := compareMarshal(expectedReq, string(data)); err != nil {
		t.Fatalf(err.Error())
	}
}