+ Start a Discussion
wixxeywixxey 

Test Code for the Class

Kindly help me in writing a test code for this class .. i am new to visualforce

 

public class geoIp{
    public String host{set;get;}
    public String getResult{get;set;}
    
    public PageReference submit() {
 if (host.length() == 0) {
                ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'City cannot be blank'));
            }
     
            if (!ApexPages.hasMessages())
                getResult=getIp(host);
     
            return null;
       }
     public String getIP(String vHost)
       {
           HttpRequest req= new HttpRequest();
           Http http = new Http();
           req.setMethod('GET');
           String url = 'http://freegeoip.net/json/' + EncodingUtil.urlEncode(vHost,'UTF-8') + '';
           req.setEndpoint(url);
           HttpResponse res = http.send(req);
           String json =  res.getBody().replace('\n','');
           
            try {
                JSONObject j = new JSONObject( json );
                return parseJson(j);
            } catch (JSONObject.JSONException e) {
                return 'Error parsing JSON response from Google: '+e;
            }
           
       }
       
       public String parseJson(JSONObject resp){
           String detail = resp.getString('ip') + '   ' + resp.getString('country_code') + '   ' + resp.getString('country_name') + '   ' + resp.getString('region_code') + '   ' + resp.getString('region_name') +  '   ' + resp.getString('latitude') + '   ' + resp.getString('longitude') + '   ' + resp.getString('zipcode') + '   ' + resp.getString('areacode');
             
           return detail;
       }
}

ManjunathManjunath

Hi,

 

Follow this link it will help you create testclass  http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm

 

Regards,