Casperjs - Cookies are not enabled after thenOpen - cookies

I'm trying to look for a resource in the dom, in order to that I'm following each link of the current page.
The problem is that the first time works perfectly well, but when I start to follow the links I receive "cookies are not enabled".
I call the script like this: casperjs --cookies-file=/tmp/mycookies.txt script.js
Here is the script:
var casper = require('casper').create({
logLevel: "debug", // Only "info" level messages will be logged
verbose:true,
onError: function(self, m) { // Any "error" level message will be written
console.log('FATAL:' + m); // on the console output and PhantomJS will
self.exit(); // terminate
},
pageSettings: {
javascriptEnabled: true,
loadImages: false, // The WebPage instance used by Casper will
loadPlugins: false, // use these settings
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
}
});
var fs = require('fs');
casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4');
phantom.cookiesEnabled = true;
var x = require('casper').selectXPath;
function getLinks() {
var links = document.querySelectorAll('a');
return Array.prototype.map.call(links, function(e) {
return e.getAttribute('href')
});
}
function followLinks(links) {
casper.each(links, function(self, link) {
self.thenOpen(link, function() {
fs.write("pages/" + link+ ".html", this.getHTML(), 'w'); //for debugging
if (!lookFor(x('//input[#name="Export"]'))) {
var links = getLinks();
followLinks(links);
}
});
});
}
casper.start('http://www.url.com', function() {
this.fill('form#formAuth', {
login: 'user#url.com',
password: 'apass'
}, true);
});
casper.then(function() {
links = links.concat(this.evaluate(getLinks));
followLinks(links);
});
casper.run(function() {
this.exit();
});

The problem was that one of the links is the Logout Link :D. That's why suddenly I got the error

Related

AWS lambda HTTP API only receiving header from POST form

I am trying to send a name and email from a static website to a lambda function through a HTTPS gateway. Here's the JS code that handles the inputs.
function submitToAPI(e) {
e.preventDefault();
var URL = "https://fvxw67h6bl.execute-api.us-east-1.amazonaws.com/default/GetData";
let name = (document.getElementById("name") as HTMLInputElement).value;
let email = (document.getElementById("email") as HTMLInputElement).value;
if (name=="" || email=="")
{
alert("Please Fill All Required Field");
return false;
}
const emailRE = /^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(!emailRE.test(email)) {
alert("Email Address entered, is not valid");
return false;
}
var data = {
name : name,
email : email
};
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", URL);
xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.send(JSON.stringify(data));
}
I was trying to access the data in the lambda function with:
name = event["name"]
email = event["email"]
But it kept returning errors.
So I tried printing the event object and this is what it returned.
{
'version': '2.0',
'routeKey': 'ANY /GetData',
'rawPath': '/default/GetData',
'rawQueryString': '',
'headers': {
'accept': '*/*',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
'content-length': '0',
'host': 'fvxw67h6bl.execute-api.us-east-1.amazonaws.com',
'origin': 'http://localhost:3000',
'referer': 'http://localhost:3000/',
'sec-ch-ua': '"Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"',
'sec-ch-ua-mobile': '?0',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'cross-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36',
'x-amzn-trace-id': 'Root=1-611f1788-7c27d190689e2be22374fd1f',
'x-forwarded-for': '107.2.126.157',
'x-forwarded-port': '443',
'x-forwarded-proto': 'https'
},
'requestContext': {
'accountId': '551656018604',
'apiId': 'fvxw67h6bl',
'domainName': 'fvxw67h6bl.execute-api.us-east-1.amazonaws.com',
'domainPrefix': 'fvxw67h6bl',
'http': {
'method': 'POST',
'path': '/default/GetData',
'protocol': 'HTTP/1.1',
'sourceIp': '107.2.126.157',
'userAgent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'
},
'requestId': 'EWCdaiwUIAMEVOA=',
'routeKey': 'ANY /GetData',
'stage': 'default',
'time': '20/Aug/2021:02:46:32 +0000',
'timeEpoch': 1629427592825
},
'isBase64Encoded': False
}
Basically it just returned the header.
How would I access the data I sent through the API?

Configuring CSRF tokens with apollo client and graphene-django

I am having trouble properly setting up csrf tokens in the authlink header.
const authLink = setContext((_, { headers }) => {
const token = localStorage.getItem(AUTH_TOKEN)
return {
"headers": {
'X-CSRFToken' : getCookie('csrftoken'),
"Authorization": token ? `JWT ${token}` : '',
...headers,
},
};
});
The request being sent looks ok from the browser devtools, as you can see at the bottom the csrf token looks right? I cleared my browser data to make sure it wasn't old, but I'm not sure if that's effective anyways.
accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Authorization
Connection: keep-alive
Content-Length: 505
content-type: application/json
Host: localhost:8000
Origin: http://localhost:3000
Referer: http://localhost:3000/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.63
X-CSRFToken: LsV83sz3Rb5RRIlNcRN3AgnniodwsSMpvXwMGquPGRbvoPpISfKv6MBEf86rVzVp
The error I get through the site is
CSRF verification failed. Request aborted
my django server shows
Forbidden (CSRF cookie not set.)
Have you tried to install corsheaders? or are you sure you have this function
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
I had same issue because x-csrftoken was not transmitted in the Request Headers
Here is the fix i have done
On my React code
import { ApolloClient, createHttpLink, InMemoryCache } from '#apollo/client';
import { setContext } from '#apollo/client/link/context';
import Cookie from "js-cookie";
const httpLink = createHttpLink({
uri: '/graphql/',
});
const authLink = setContext((_, { headers }) => {
return {
headers: {
...headers,
"x-csrftoken": Cookie.get("csrftoken")
}
}
});
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache()
});

How make local http request in flutter?

I'm making an app, and i already have a server running in my local. I can acess trought my navigator the url:
Request URL: http://localhost:4444/categorie?page_size=15&page=1
Request Method: GET
Status Code: 200 OK
Remote Address: 127.0.0.1:4444
Referrer Policy: no-referrer-when-downgrade
Preview:
{
"count": 4,
"next": null,
"previous": null,
"results": [
{
"uid": "_b656062d3754",
"name": "Pinga"
},
{
"uid": "_0c644473c244",
"name": "Cerveja"
},
{
"uid": "_75df557ccbaa",
"name": "Vinhos"
},
{
"uid": "_8e32ce3baded",
"name": "Refrigerantes"
}
]
}
But when i try in flutter, the request never respond:
getCategories() async {
String url = 'http://localhost:4444/categorie?page_size=15&page=1';
var response = await http.get(url);
if (response.statusCode == 200) {
// If the call to the server was successful, parse the JSON
return response.body;
} else {
// If that call was not successful, throw an error.
throw Exception('Failed to load post');
}
}
I'm running the server in my Windows10 using Django. And my flutter app is in the Android Studio using a Virtual Machine as device.
I tryed to change the ip to 127.0.0.1 and 10.0.0.1 but still not working. Have i change the host of Django or enable any configuration in my flutter or inside VirutalBox?
Request to other sites is working well. The problem is in local.
My dependencies:
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
http: any
dev_dependencies:
flutter_test:
sdk: flutter
Your localhost on your PC is not the same on your Android emulator. To access your PC's localhost from the Android emulator, you need to point to 10.0.2.2.
This means that you need to change your code to:
String url = 'http://10.0.2.2:4444/categorie?page_size=15&page=1';
Use this in pubspec file under dependencies:
http: ^0.12.0+2
Use this method for Http request
getCategories() async {
final response = await http.get("http://localhost:4444/categorie?page_size=15&page=1",
headers: {
'content-type': 'application/json',
},
);
if (response.statusCode == 200) {
final result = json.decode(response.body);
var list = result['results'] as List;
// make a model class
List<Categories> categories_= list.map((i) => Categories.fromJson(i)).toList();
setState(() => {
// update value
});
} else {
setState(() => {
// Show error
});
}
}
Model class:
class Categories{
String uid;
String name;
Categories({this.uid, this.name});
Categories.fromJson(Map<String, dynamic> json) {
uid = json['uid'];
name = json['name'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['uid'] = this.uid;
data['name'] = this.name;
return data;
}
}

Redirect Page When Onclick Pushnotifications

I am using FCM PLugin Ionic 2
Below my app component
constructor(public menu1: MenuController,public alertCtrl:AlertController,public platform: Platform,public authservice:Authservice) {
this.initializeApp();
}
initializeApp() {
this.platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
Splashscreen.hide();
//Push Notifications
if(typeof(FCMPlugin) !== "undefined")
{
FCMPlugin.getToken(function(t){
console.log("Use this token for sending device specific messages\nToken: " + t);
}, function(e){
console.log("Uh-Oh!\n"+e);
});
FCMPlugin.onNotification(function(d){
if(d.wasTapped){
console.log(d);
var nav:NavController = this.app.getComponent("nav");
this.nav = nav;
nav.setRoot(MycomplaintsPage);
} else {
console.log("Push Notification", d);
console.log("you have new notifications");
}
}, function(msg){
}, function(err){
console.log("Arf, no good mate... " + err);
});
}
}
It's work Perfectly.But Click on Notifications Page cannot redirect.
I am getting this error
Uncaught TypeError: Cannot read property 'getComponent' of undefined.
How to fix this issue.
Kindly Advice me,
Thanks
The this object changes if you use regular functions i.e it belongs to the function not the class so this.app is nor defined. Arrow functions ()=>{} doesn't define this and the outer this is used.
Use arrow functions for callbacks
FCMPlugin.onNotification((d)=>{
if(d.wasTapped){
console.log(d);
var nav:NavController = this.app.getComponent("nav");
this.nav = nav;
nav.setRoot(MycomplaintsPage);
//...
});
Or set self=this before the call and use self.
let self = this.//declare here
FCMPlugin.onNotification(function(d){
if(d.wasTapped){
console.log(d);
var nav:NavController = self.app.getComponent("nav");//use here
this.nav = nav;
nav.setRoot(MycomplaintsPage);
//...
});

Trying to mock $http in Angular

I am trying to create some basic test coverage of a service that I have created. Here is my service:
App.factory('encounterService', function ($resource, $rootScope) {
return {
encounters: [],
encountersTotalCount: 0,
encountersIndex: 0,
resource: $resource('/encounters/:encounterId', {encounterId:'#encounterId'}, {
search: {
method: 'GET',
headers: {
'RemoteUser': 'jjjyyy',
'Content-Type': 'application/json'
}
}
}),
getMoreEncounters: function() {
var that = this;
that.resource.search({}, function(data) {
that.encountersTotalCount = data.metadata.totalCount;
_.each(data.encounters, function(encounter) {
that.encounters.push(encounter);
});
that.busy = false;
that.offset += 10;
$rootScope.$broadcast('encountersFetched');
});
}
};
});
Here is my test that I cannot get to work:
describe('encounterService', function() {
var _encounterService, httpBackend;
beforeEach(inject(function(encounterService, $httpBackend) {
_encounterService = encounterService;
httpBackend = $httpBackend;
var url = '/encounters/';
httpBackend.when('GET', url).respond([{}, {}, {}]);
}));
afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
it('should return a list of encounters', function() {
_encounterService.getMoreEncounters();
httpBackend.flush();
expect(_encounterService.encounters.size).toBe(3);
});
});
The error I get is
Chrome 31.0.1650 (Mac OS X 10.8.5) Clinical App services encounterService should return a list of encounters FAILED
Error: Unexpected request: GET encounters
No more request expected
at $httpBackend (/Users/mhamm/Developer/clinical/app/bower_components/angular-mocks/angular-mocks.js:1179:9)
at sendReq (/Users/mhamm/Developer/clinical/app/bower_components/angular/angular.js:7611:9)
at $http.serverRequest (/Users/mhamm/Developer/clinical/app/bower_components/angular/angular.js:7345:16)
at wrappedCallback (/Users/mhamm/Developer/clinical/app/bower_components/angular/angular.js:10549:81)
at wrappedCallback (/Users/mhamm/Developer/clinical/app/bower_components/angular/angular.js:10549:81)
at /Users/mhamm/Developer/clinical/app/bower_components/angular/angular.js:10635:26
at Scope.$eval (/Users/mhamm/Developer/clinical/app/bower_components/angular/angular.js:11528:28)
at Scope.$digest (/Users/mhamm/Developer/clinical/app/bower_components/angular/angular.js:11373:31)
at Function.$httpBackend.flush (/Users/mhamm/Developer/clinical/app/bower_components/angular-mocks/angular-mocks.js:1453:16)
at null.<anonymous> (/Users/mhamm/Developer/clinical/test/spec/clinical.spec.js:78:21)
Error: [$rootScope:inprog] $digest already in progress
http://errors.angularjs.org/1.2.0/$rootScope/inprog?p0=%24digest
at /Users/mhamm/Developer/clinical/app/bower_components/angular/angular.js:78:12
at beginPhase (/Users/mhamm/Developer/clinical/app/bower_components/angular/angular.js:11830:15)
at Scope.$digest (/Users/mhamm/Developer/clinical/app/bower_components/angular/angular.js:11364:9)
at Function.$httpBackend.verifyNoOutstandingExpectation (/Users/mhamm/Developer/clinical/app/bower_components/angular-mocks/angular-mocks.js:1486:16)
at null.<anonymous> (/Users/mhamm/Developer/clinical/test/spec/clinical.spec.js:68:21)
I do not fully understand mocking, so I am sure I am doing something basic incorrectly. Please show me what I am doing wrong.
$resource automatically removes the trailing slashes from the url.
From version 1.3.0 there is a fourth argument that allows you to set stripTrailingSlashes: false to keep those.