+ Start a Discussion
Matt MetrosMatt Metros 

System.XmlException: start tag unexpected character \ua0

I am receiving the following error from my HttpCallOutMock Class:

System.XmlException: start tag unexpected character \ua0 (position: START_DOCUMENT seen <rss\ua0... @1:5)
 
@isTest
global class getRSSFeedMockCallout implements HttpCalloutMock 
{

  global HttpResponse respond(HttpRequest request)
  {
      String sessionBody = '<rss xmlns:media=\"http://search.yahoo.com/mrss/\" version=\"2.0\">' +
      '<channel>'+
        '<item>'+
          '<title>Draymond Green Is a SoulCycle Devotee - Bicycling</title>'+
          '<link>https://www.bicycling.com/news/a27024393/draymond-green-soulcycle/</link>'+
          '<pubDate>Wed, 03 Apr 2019 12:00:00 GMT</pubDate>'+
          '<description><a href=\"https://www.bicycling.com/news/a27024393/draymond-green-soulcycle/\" target=\"_blank\">Draymond Green Is a SoulCycle Devotee</a>&nbsp;&nbsp;<font color=\"#6f6f6f\">Bicycling</font><p>If you take SoulCycle classes in the Bay Area, and you notice one extremely tall guy in the room, you may be looking at Golden State Warriors star Draymond ...</p></description>'+
          '<source url=\"https://www.bicycling.com\">Bicycling</source>'+
          '<media:contenturl=\"https://lh5.googleusercontent.com/proxy/QvLCgDJMIqLENGFBUt1CCoUPO1XB6gJkGFPV5FuM0QKfyaG8WdhuHGtfcVaTzNzPAYyHo-arnqUBkWkZ8s7-p7WBUbKlfJ4xr0hl8hltQWsBCG0yDDh13Ul5qKrZgLvjQitRecmu=-w150-h150-c\" medium=\"image\" width=\"150\"height=\"150\"/>'+
        '</item>'+
      '</channel>'+
    '</rss>';


      HttpResponse res = new HttpResponse();
      res.setHeader('Content-Type', 'text/xml');
      res.setBody( sessionBody );
      res.setStatusCode(200);
      return res;
  }   
}

This is my apex class that is requesting.
 
public with sharing class getRSSFeed {

	@AuraEnabled
	public static List<Map<String,String>> getCompanyRSSFeed(String url, String recordID) {
		
		String recordName = getRecordName(recordID);

		List<Map<String,String>> newsArticles = search(url, recordName);

		return newsArticles;
	}
	
	@AuraEnabled
	public static String getRecordName(String recordID)
	{
		String accountRecordID = recordID;
		return [SELECT Name From Account WHERE ID = : accountRecordID limit 1].Name;
	}

	@AuraEnabled
	public static  List<Map<String,String>> search(String remoteSiteURL, String query)
	{
		String url = remoteSiteURL + EncodingUtil.urlEncode(query, 'UTF-8');

		Http h = new Http();
		HttpRequest req = new HttpRequest();
		HttpResponse res = new HttpResponse();
		if (!Test.IsRunningTest())
		{
		    req.setEndpoint(url);
	    	req.setMethod('GET');
		    res = h.send(req);
		}
		else
		{
			 getRSSFeedMockCallout mock = new getRSSFeedMockCallout();
             res = mock.respond(req);
		}


	    while (res.getStatusCode() == 302) {
		    req.setEndpoint(res.getHeader('Location'));
		    System.debug(res.getHeader('Location'));
		    res = new Http().send(req);
		}

	    Dom.Document doc = res.getBodyDocument();
	    Dom.XMLNode root = doc.getRootElement();

	   	Dom.XMLNode bodyNode = root.getChildElement('channel', null);

	   	//Dom.XMLNode bodyNode = bodyNode.getChildElement('');
		List<Map<String,String>> newsStories = new List<Map<String,String>>();
	   	Integer sizeOfList = 20;
	   	Integer i = 0;


	    for(DOM.XmlNode x : bodyNode.getChildren())
	    {
	    	//System.debug(x.getName());
	    	//System.debug(x.getText());
	    	if(x.getName() == 'item' &&
	    		i != sizeOfList)
	    	{
	    		Map<String,String> story = new Map<String, String>();
	    		story.put('publisher', '');
	    		story.put('date', '');
	    		story.put('link', '');
	    		story.put('image', '');
	    		story.put('title', '');
	    		story.put('description', '');


	    		for(DOM.XmlNode itemChildren : x.getChildElements())
	    		{
	    			System.debug(itemChildren.getName());

	    			//System.debug(itemChildren.getName());
	    			//System.debug(itemChildren.getText());
	    			if(itemChildren.getName() == 'link')
	    			{
	    				story.put('link', itemChildren.getText());
	    			}
	    			else if(itemChildren.getName() == 'title')
	    			{
	    				story.put('title', itemChildren.getText());
	    			}
	    			else if(itemChildren.getName() == 'pubDate')
	    			{
	    				story.put('date', itemChildren.getText());
	    			}
	    			else if(itemChildren.getName() == 'description')
	    			{

	    				String stripTags = itemChildren.getText();
	    				List<String> description = stripTags.split('(<.*?>)');
	    				story.put('description', description[5]);
	    			}
	    			else if(itemChildren.getName() == 'content')
	    			{
	    				story.put('image', itemChildren.getAttributeValue(itemChildren.getAttributeKeyAt(0), itemChildren.getAttributeKeyNsAt(0)));
	    			}
	    			else if(itemChildren.getName() == 'source')
	    			{
	    				story.put('publisher', itemChildren.getText());
	    			}


	    		}

	    		newsStories.add(story);
	    		i += 1;

	    	}

	    }
	    return newsStories;
	}


}